You need to sign in to do that
Don't have an account?
Mohammad 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
Sample Apex Code:
Thanks in Advance...
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...
All Answers