Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to use Json.Deserialize Apex properly?

Asked by Christopher Paterson Jul 31, 2023 2.2K views 1 answer
Share

About this question

What is the way to deserialize the json object mentioned below:


 {
    "response": {
        "count": 1,
        "benchmark": 0.22567009925842,
        "requests": [
            {
                "request": {
                    "id": 537481,
                    "image_thumbnail": "",
                    "title": "Request for new bin(s) - residential",
                    "description": "Propmain ref  3234-1114",
                    "status": "submitted",
                    "address": "36 Pine Tree Close",
                    "location": "Peterborough, England",
                    "zipcode": "PE1 1EJ",
                    "user": "",
                    "date_created": 1417173208,
                    "count_comments": 0,
                    "count_followers": 0,
                    "count_supporters": 0,
                    "lat": 52.599967,
                    "lon": -0.233482,
                    "user_follows": 0,
                    "user_comments": 0,
                    "user_request": 1,
                    "rank": "0"
                }
            }
        ],
        "status": {
            "type": "success",
            "message": "Success",
            "code": 200,
            "code_message": "Ok"
        }
    }
}
What i've tried:
 Map rawObj = (Map) JSON.deserializeUntyped(jsonString);
    Map responseObj = (Map)rawObj.get('response');
    List<Object> reqs = (List<Object>) responseObj.get('requests');
    System.debug('Map Size = ' + reqs.size());
    Map i = new Map ();
    for (Object x : reqs) {
         i = (Map)x;
}
    Map requests = (Map)i.get('request');
    System.debug('Map Size = ' + i.size());
    for (String field : i.keySet()){
        Object id = i.get(field);
        Object title = i.get('title');
        System.debug('Id : ' + id);
        System.debug('title : ' + title);
        //System.debug('Title : ' + title);
      }


Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.