1

This is my JSON

{
"user":{  
      "self":"https://my.atlassian.net/rest/api/2/user?username=admin",
      "name":"admin",
      "key":"admin",
      "emailAddress":"[email protected]",
      "avatarUrls":{  
         "48x48":"https://my.atlassian.net/secure/useravatar?ownerId=admin&avatarId=10500",
         "24x24":"https://my.atlassian.net/secure/useravatar?size=small&ownerId=admin&avatarId=10500",
         "16x16":"https://my.atlassian.net/secure/useravatar?size=xsmall&ownerId=admin&avatarId=10500",
         "32x32":"https://my.atlassian.net/secure/useravatar?size=medium&ownerId=admin&avatarId=10500"
      },
      "displayName":"Ratan[Administrator]",
      "active":true,
      "timeZone":"Asia/Kolkata"
   }
}

My fav site for converting into Apex https://www.adminbooster.com/tool/json2apex

Generated apex class.

public class fromJSON{
    public cls_user user;
    class cls_user {
        public String self; 
        public String name; 
        public String key;  
        public String emailAddress; 
        public cls_avatarUrls avatarUrls;
        public String displayName;  
        public boolean active;
        public String timeZone;
    }
    class cls_avatarUrls {
        public String 48x48; // profile picture size    
        public String 24x24;    
        public String 16x16;    
        public String 32x32;    
    }
    public static fromJSON parse(String json){
        return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
    }
}

Now my problem is below

class cls_avatarUrls {
            public String 48x48; // profile picture size    
            public String 24x24;    
            public String 16x16;    
            public String 32x32;    
        }

If I try to save then receives unexpected token: '48'

I know we can't use integer 48x48 here.

So is there way to solve this issue/problem

Thanks in advance.

3 Answers 3

3

To solve your problem you can declare avatarUrls as a Map.

Code Example

public class fromJSON{
    public cls_user user;
    class cls_user {
        public String self; 
        public String name; 
        public String key;  
        public String emailAddress; 
        public Map<String,String> avatarUrls;
        public String displayName;  
        public boolean active;
        public String timeZone;
    }

    public static fromJSON parse(String json){
        return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
    }
}

Regards.

0
2

You'll want to use JSON.deserializedUntyped to generate a map. Unlike concrete classes, maps can hold settings that start with numbers.

3
  • Yes I can use JSON.deserializedUntyped but I don't want to use reason my JSON data is nearly 500 line of code. So its better to use Apex class.
    – Ratan Paul
    Commented Nov 26, 2015 at 8:58
  • 1
    @Ratan Trust me, you'll find that the only other solutions are at least as difficult to use. You want to deserialize into a generic map.
    – sfdcfox
    Commented Nov 26, 2015 at 9:56
  • Thanks @sfdcfox. I will remember your solution for future use.
    – Ratan Paul
    Commented Nov 26, 2015 at 10:05
1

In Salesforce, you cannot have variables starting with a number. While you can add an underscore to the beginning of the variable, your automatic JSON parsing will fail to assign values to those variables

If possible, the ideal is to modify the original JSON. If not, you can use a "Search & Replace" (you can use string methods within SFDC) to manipulate the received JSON so those variable names match the classes.

Unless you have a huge JSON payload (which may blow your heap) you should be fine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .