+ Start a Discussion
SUMIT BANERJEESUMIT BANERJEE 

Error: Compile Error: unexpected token: '-' at line 18

Hi, I generate class as per JSON response, now I am facing issue Like unexpected token: '-' at line 18 and 30, How to resolve this issue and the  API name is access-token, I can't change that


public class TokenResponse {
    public static void consumeObject(JSONParser parser) {
        Integer depth = 0;
        do {
            JSONToken curr = parser.getCurrentToken();
            if (curr == JSONToken.START_OBJECT || 
                curr == JSONToken.START_ARRAY) {
                depth++;
            } else if (curr == JSONToken.END_OBJECT ||
                curr == JSONToken.END_ARRAY) {
                depth--;
            }
        } while (depth > 0 && parser.nextToken() != null);
    }

    public String type_Z {get;set;} 
    public Integer state {get;set;} 
    public String access-token {get;set;} 

    public TokenResponse(JSONParser parser) {
        while (parser.nextToken() != JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != JSONToken.VALUE_NULL) {
                    if (text == 'type') {
                        type_Z = parser.getText();
                    } else if (text == 'state') {
                        state = parser.getIntegerValue();
                    } else if (text == 'accesstoken') {
                        access-token = parser.getText();
                    } else {
                        System.debug(LoggingLevel.WARN, 'Root consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
    
    
    public static TokenResponse parse(String json) {
        return new TokenResponse(System.JSON.createParser(json));
    }
}
Amit Singh 1Amit Singh 1
Hi Sumit,

The error is because of "access-token" variable you have declared in your class. You need to change the variable name and your error will be resolved. You can not use - in any variable name. Use a variable name like access_token, access_ or _accessToken.
 
Hope this make sense :)
Thanks,
Amit Singh.
Amit Chaudhary 8Amit Chaudhary 8
Please remove '-' and try '_' (unserscope )