2

sample JSON string :

'{'+
            '   \"count\":\"1\",'+
            '   \"querycount\":\"1\",'+
            '   \"ranking\":\"adaptive\",'+
            '   \"mindPage\":{'+                  ----->able to access id, res from mindPage
            '       \"id\":\"1\",'+
            '       \"res\":\"1\",'+
            '       \"properties\":{'+            ------>able to access count, href from properties
            '         \"@count\":\"2\",'+
            '         \"href\":\"www.google.com",'+
            '         \"property\":['+          ----->Not able to access its values
            '            {'+
            '               \"revision\":\"2\",'+
            '               \"resid\":\"259\",'+
            '               \"name\":\"mindtouch.page#overview\",'+
            '               \"href\":\"www.google.com",'+
            '               \"etag\":\"259.r2_ts2016-06-20T12:35:33Z\",'+
            '               \"resource-is-deleted\":\"false\",'+
            '               \"resource-rev-is-deleted\":\"false\",'+
            '               \"change-description\":\"page overview\",'+
            '               },'+
            '           },'+
            '        },'+
            '   \"parsedQuery\":\"+(content:(\\\"product2\\\") title:(\\\"product2\\\")^10 path.title:(\\\"product2\\\")^4 '+
  '}';

Apex DeserializeUntyped Map methods:

Map<String, Object> dim = (Map<String, Object>)m.get('mindPage');  //able to get the values of main page
system.debug(' Mind Page Id '+dim.get('id'));
Map<String, Object> properties = (Map<String, Object>)dim.get('properties');   //able to get the values of sub page
system.debug(' Mind Page Id '+properties.get('href'));
Map<String, Object> property = (Map<String, Object>)properties.get('property');   //Error - not able to get the values of second subpage(has array)

Error : System.TypeException: Invalid conversion from runtime type List to Map

Any solution for this?

1

You will have to use list for arrays in your JSON .Something like below

list<Object> lstproperty = (list<Object>)properties.get('property'); 
//Now loop over this 
for(Object propertyObj:lstproperty){
    Map<String, Object> property = (Map<String, Object>)propertyObj;
}
  • 1
    Issue solved. Thank you very much Mohith. Thums Up – sfdcDev Jun 22 '16 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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