[SalesForce] How to deserialize a JSON String to Apex

How can I deserialize this json object:

    {
    "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<String,Object> rawObj = (Map<String,Object>) JSON.deserializeUntyped(jsonString);
    Map<String,Object> responseObj = (Map<String,Object>)rawObj.get('response');
    List<Object> reqs = (List<Object>) responseObj.get('requests');
    System.debug('Map Size = ' + reqs.size());
    Map<String, Object> i = new Map<String, Object> ();
    for (Object x : reqs) {
         i = (Map<String, Object>)x;
}
    Map<String,Object> requests = (Map<String,Object>)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);
      }

Best Answer

I suggest you paste your JSON into http://json2apex.herokuapp.com/ and try the generated code. This tool generates simple Apex classes with a field per JSON field and then you can parse with a single JSON.deserialize call.

Here is the code produced from your JSON. (You can rename the classes as you see fit and also change data types if you know better e.g. use Decimal instead of Double if you are dealing with money.)

//
// 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> 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);
    }
}
Related Topic