How to use Json.Deserialize Apex properly?

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);
      }


Answered by Diane Carr

 You need to paste the JSON into http://json2apex.herokuapp.com/ and use the generated code. The tool produces Apex classes including a field per JSON field and then you will be able to parse with one JSON.deserialize.apex call. The code given below created from the JSON. It is possible to rename the classes and alter the data types. The code is given below:

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//
public class JSON2Apex {
    public class Status {
        public String type;
        public String message;
        public Integer code;
        public String code_message;
    }
    public class Requests {
        public Request request;
    }
    public Response response;
    public class Response {
        public Integer count;
        public Double benchmark;
        public List requests;
        public Status status;
    }
    public class Request {
        public Integer id;
        public String image_thumbnail;
        public String title;
        public String description;
        public String status;
        public String address;
        public String location;
        public String zipcode;
        public String user;
        public Integer date_created;
        public Integer count_comments;
        public Integer count_followers;
        public Integer count_supporters;
        public Double lat;
        public Double lon;
        public Integer user_follows;
        public Integer user_comments;
        public Integer user_request;
        public String rank;
    }
    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
    static testMethod void testParse() {
        String json = '{'+
        '"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"'+
        ' }'+
        '}'+
        '}';
        JSON2Apex obj = parse(json);
        System.assert(obj != null);
    }
}

The simplest technique to parse JSON data in Apex is by deploying the built-in JSON.deserialize() method. It consumes a JSon string and a class type as arguments and returns the object of the given class type.

The Salesforce Certification Training offered at JanBask Training provides experience like offline classes to help candidates master the Salesforce Development and Administration in a better way to face the tough job market scenario. The course guarantees job success and lets you qualify the certification exam through intensive, and expert-led virtual sessions and hands-on practical assignments. The course will teach you to create and configure the salesforce account to help you gather, extract, and examine the data relevant to the customer base. You can also avail the Force.com platform to create and use the advanced cloud apps with utmost confidence.



Your Answer

Interviews

Parent Categories