JSON.deserializeUntyped() not parsing the whole JSON

You can make the code work with an extra step:

String s = '{"totalResults": 2, "startIndex":0, "pricing":[{"price":10.80,"cost":9.22,"gp":12},{"price":5.50,"cost":4.00,"gp":24}]}';
Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(s);
List<Object> pricing = (List<Object>) m.get('pricing');
for (Object o : pricing) {
    Map<String, Object> p = (Map<String, Object>) o;
    System.debug('>>> ' + p);
}

In these situations I find it much more ... less frustrating to deserialize to a known type.

Given an exemplar JSON string, you can input it here: http://json2apex.herokuapp.com/ and this will generate classes and tests to deserialize the json. Very handy.

Tags:

Json

Apex