Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am trying to parse a complex JSON String using JSON.deserializeUntyped(). It parses the first level elements correctly but the second level elements come back as type ANY, even though I am expecting it to be another Map. Following is a sample JSON String.

{
    "totalResults": 2,
    "startIndex":0,
    "pricing":[
        {
            "price":10.80,
            "cost":9.22,
            "gp":12
        },
        {
            "price":5.50,
            "cost":4.00,
            "gp":24
        }
    ]
}

My Apex code is

Map<String, Object> resultsMap = (Map<String, Object>) JSON.deserializeUntyped(jsonStr);

List<Map<String, Object>> pricing = (List<Map<String, Object>>)resultsMap.get('pricing');

I get an error on the line 2.

Invalid conversion from runtime type LIST to LIST>

What am I doing wrong? Or what is correct way of getting the list of prices?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

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);
}
share|improve this answer
    
this works. thanks @Keith. –  user593644 May 9 at 14:27

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.

share|improve this answer
    
I don't want to create a concrete class for the JSON String. The reason being that the JSON response coming from external service is expected to change quite a few times in next few months. I am trying to keep things flexible in Apex so we don't have to make any changes in Apex classes whenever JSON changes. Designers can use the Map from deserialized JSON on the visualforce page. –  user593644 May 9 at 13:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.