You need to sign in to do that
Don't have an account?
How To Loop Through Parsed JSON
Hi All, I'm looking for some advice on best practice. I want to loop through Options and I've come up with the following solution which actually works. What I want to know is whether or not my method of a loop within a loop is best practice or if there is a better way?
String json = '{'+
' \"TrackingCategories\": ['+
' {'+
' \"Name\": \"Region\",'+
' \"Status\": \"ACTIVE\",'+
' \"TrackingCategoryID\": \"351953c4-8127-4009-88c3-f9cd8c9cbe9f\",'+
' \"Options\": ['+
' {'+
' \"TrackingOptionID\": \"ce205173-7387-4651-9726-2cf4c5405ba2\",'+
' \"Name\": \"Eastside\",'+
' \"Status\": \"ACTIVE\"'+
' },'+
' {'+
' \"TrackingOptionID\": \"6eb12fdf-63de-4033-98df-be679d84e3c2\",'+
' \"Name\": \"North\",'+
' \"Status\": \"ACTIVE\"'+
' },'+
' {'+
' \"TrackingOptionID\": \"6159bdd4-b634-4338-a664-e929aa73f70f\",'+
' \"Name\": \"South\",'+
' \"Status\": \"ACTIVE\"'+
' },'+
' {'+
' \"TrackingOptionID\": \"161ad543-97ab-4436-8213-e0d794b1ea90\",'+
' \"Name\": \"West Coast\",'+
' \"Status\": \"ACTIVE\"'+
' }'+
' ]'+
' }'+
' ]'+
'}';
TrackingOptionsResponse obj = TrackingOptionsResponse.parse(json);
System.debug('obj='+obj);
for(TrackingOptionsResponse.TrackingCategories r :obj.TrackingCategories){
system.debug('r='+r.Name);
System.debug('r2='+r.Options);
for(TrackingOptionsResponse.Options l :r.options){
System.debug('l='+l.Name);
}
}
Thanks,
Dan
For the above data Loop inside loop is fine only... It's small amount of data... If it is a trigger you have to worry about loop inside loop...
And you can use the below way also for the above JSON instead of more looping:
Thanks,
Maharajan.C
All Answers
For the above data Loop inside loop is fine only... It's small amount of data... If it is a trigger you have to worry about loop inside loop...
And you can use the below way also for the above JSON instead of more looping:
Thanks,
Maharajan.C