3

How to deserialize the below json ? I want to retrieve messages only in list of string .

{
"posts": {
"data": [
  {
    "message": "Hello. My name is sdfd. How can I help you?",
    "created_time": "2016-11-16T06:45:52+0000",
    "id": "1832195760359548_1832134398493692608"
  },
  {
    "message": "Hi,welcome to ddd.",
    "created_time": "2016-11-16T06:40:56+0000",
    "id": "1832195760359548_18321978373423026007"
  },
  {
    "story": "kjkj updated their profile picture.",
    "created_time": "2016-11-16T06:37:32+0000",
    "id": "18321957603594344548_1832197183692739"
  }
],
"paging": {
  "previous": "https://graph.facebook.com/v2.8/183219576034559548/posts?since=1479278752&access_token=EAACEdEose0cBABWmn8IwQkAhA34Il5eO5BvBonlKXnt7cPz2LAwnxXDSTSLgkaWL1CxCAyYZAjrHUQZBqC20wL9R7NuW7Mnrc3ZBMEwVjdfdf2W2PQMM1a2Y46lfNwxZBRQX7j5exZBHPNazan7uGsc3T3Qi7CZBlffRjvuig4KgC0k3OzVIIyeIGM&limit=25&__paging_token=enc_AdBdMzcB2oGURgtG7wm2L89Qhb5se42mxZAQvklD1fAZC5MlGYZAD1ZCbVceMDveZBmEOjVWrbvMxmPbAiDSdUfOOlvanZAW4RnByhuI0Ouw8ZASWZCDRQZDZD&__previous=1",
  "next": "https://graph.facebook.com/v2.8/183219576034559548/posts?access_token=EAACEdEose0cBABWmn8IwQkAhA34Il5eO5BvBonlKXnfdfdft7cPz2LAwnxXDSTSLgkaWL1CxCAyYZAjrHUQZBqC20wL9R7NuW7Mnrc3ZBMEwVj2W2PQMM1a2Y46lfNwxZBRQX7j5exZBHPNazan7uGsc3T3Qi7CZBlffRjvuig4KgC0k3OzVIIyeIGM&limit=25&until=1479278252&__paging_token=enc_AdBPW0qUekoNQXqvPa1dXT2I6piNQvU2EaaIKmMpzgXZAZAszw2esNJCEZCAgtEWxBMH369YEdvkmvHZBMEJZB05ef5GRf9cQ2wBtZAjcurKr9oLIobgZDZD"
 }
},    

"id": "1832195760359548"
}
4

You could use a JSONParser and iterate through the Tokens plucking out the "message" values...

list<string> messageList = new list<String>();
JSONParser parser = JSON.createParser(JSONContent);
// Advance to the next token.
while (parser.nextToken() != null) {
    System.debug('Current token: ' +  parser.getCurrentName());
    if(parser.getCurrentName() == 'message'){
        parser.nextToken();    //We want the value, so advance to the next token.
        messageList.add(parser.getText());
    }
}
system.debug(messageList);

This pattern, while not terribly efficient, provides a loosely typed solution that won't require code changes if the structure of the input JSON changes.

8

There is one good tool available. JSON2Apex

Using that you can generate a class and using that wrapper class you can get the data.

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

    public Posts posts;
    public String id;

    public class Paging {
        public String previous;
        public String next;
    }

    public class Posts {
        public List<Data> data;
        public Paging paging;
    }

    public class Data {
        public String message;
        public String created_time;
        public String id;
        public String story;
    }


    public static JSON2Apex parse(String json) {
        return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
    }
}

Now parse your JSON in parse method and it will give you JSON2Apex instance and from that you can easily get data (message or any other value) from this.

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.