function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdeveloper9sfdeveloper9 

JSON deserializing question

We need to call a service which returns json content. The response contains salesforce reserved keywords as element/attribute names. when I try to create the binding class I am getting a compile error as date and currency are reserved keywords.

 

I know we can use deserializeUntyped or ask the service provider to make the change but trying to see if there is a clean and easy way to handle on salesforce side with binding class in place.

Below is a sample which is throwing error, Please let me know if anyone has any workarounds for this.

 

 

{
    "creditCard": {
      "id": "string",
      "firstName": "string",
      "lastName": "string",
      "cardNo": "string",
      "cvv": "string",
      "date": "string",
      "currency": "string"      
}
}

 

 

Binding class which gives compile error

 

public class CreditCardSample{

    public CreditCard creditCard;

    public class CreditCard {
        public String id;
        public String firstName;
        public String lastName;
        public String cardNo;
        public String cvv;
        public String date;
        public String currency;
    }

}

 

sfdcfoxsfdcfox
http://wiki.developerforce.com/page/Getting_Started_with_Apex_JSON

You have to use one of the generic json parsers instead of stronly typed classes. See code in the page.
gbu.varungbu.varun

If you are getting http response in Json format you should parse it through JSONParser class.

For example:

JSONParser parser = JSON.createParser(RestContext.request.requestBody.toString());

 

while(parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
System.debug(' Field Name:::: '+ parser.getText());
parser.nextToken();
System.debug(' Field Value:::: '+parser.getText());
}
}