+ Start a Discussion
Mohammad Zakir HossainMohammad Zakir Hossain 

json parser for object of array by Apex wrapper class

Hi,
I am struggling with parsing JSON for object of array values with other fields.
The problem is unable to define variable set in wrapper class for "linked_objects": { "jobcodes": [ 27251018 ] } .

According to below code it gives error:
FATAL_ERROR System.JSONException: Expected List<CalloutTSheets.linked_objects> but found { at...


Use Case :
parse json with keeping two value pair by wrapper class and json filed linked_objects is object type which contains Array set of values. So  to parse the json string , along with other values need to extract jobcodes array in defined wrapper class WOTSheetsLocWrap .

Sample JSON
"results": {
        "locations": {
            "5214198": {
                "id": 5214198,
                "addr1": "33W401 Roosevelt"
             ..
                "linked_objects": {
                    "jobcodes": [
                        27251018
                    ]
                }
            }

Sample Apex Code:
public class WOTSheetsLocWrap{
        public String id;
        public String addr1;        
        List<linked_objects> linked_objects;
    }   
    public class linked_objects{
        String [] jobcodes = new List<String>();     
    }   
......
		Map<String, String> WOTSLocMap = new Map<String, String>();

        system.debug('resLoc');

        system.debug(resLoc.getBody());

        JSONParser parserLoc = JSON.createParser(resLoc.getBody());
        parserLoc.nextToken();
        parserLoc.nextToken();
        parserLoc.nextToken();
		parserLoc.nextToken();
		parserLoc.nextToken();
        while(parserLoc.nextToken()!=NULL){
            parserLoc.nextToken();
            if(parserLoc.getCurrentToken()==JSONToken.START_OBJECT){
				CalloutTSheets.WOTSheetsLocWrap obj = (CalloutTSheets.WOTSheetsLocWrap)parserLoc.readValueAs(CalloutTSheets.WOTSheetsLocWrap.class);
				if(!String.isBlank(obj.id)) WOTSLocMap.put(obj.addr1,obj.id);
                system.debug('obj');

            }
        }
    system.debug(WOTSLocMap);


Thanks in Advance...
Best Answer chosen by Mohammad Zakir Hossain
Md. Abdur Razzak.Md. Abdur Razzak.
According to the json linked_objects is a object type. So using
linked_objects linked_objects;
instead of 
List<linked_objects> linked_objects;
may solve the error FATAL_ERROR System.JSONException: Expected List<CalloutTSheets.linked_objects> but found { at...
public class WOTSheetsLocWrap{
    public String id;
    public String addr1;        
    linked_objects linked_objects; //List<linked_objects> linked_objects;
}

 

All Answers

Md. Abdur Razzak.Md. Abdur Razzak.
According to the json linked_objects is a object type. So using
linked_objects linked_objects;
instead of 
List<linked_objects> linked_objects;
may solve the error FATAL_ERROR System.JSONException: Expected List<CalloutTSheets.linked_objects> but found { at...
public class WOTSheetsLocWrap{
    public String id;
    public String addr1;        
    linked_objects linked_objects; //List<linked_objects> linked_objects;
}

 
This was selected as the best answer
Mohammad Zakir HossainMohammad Zakir Hossain
Thanks Razzak, it worked here it my working code ..
public class WOTSheetsLocWrap{
        public String id;
        public String addr1;        
        // List<linked_objects> linked_objects;
        linked_objects linked_objects;
    }   
    public class linked_objects{
        String [] jobcodes = new List<String>();
    }