0

Good day! I have an application which gets JSON response from the server. My response looks like:

{
  "in_read": 414638,
  "count": 156,
  "out_read": 414637,
  "items": [
    {
      "date": 1449573812,
      "from_id": 1234,
      "read_state": 1,
      "user_id": 1234,
      "fwd_messages": [
        {
          "date": 1449573809,
          "user_id": 1234,
          "fwd_messages": [
            {
              "date": 1449573760,
              "user_id": 1234,
              "fwd_messages": [
                {
                  "date": 1449573729,
                  "user_id": 1234,
                  "body": "msg31"
                },
                {
                  "date": 1449573749,
                  "user_id": 1234,
                  "fwd_messages": [
                    {
                      "date": 1449573739,
                      "user_id": 1234,
                      "body": "msg321"
                    },
                    {
                      "date": 1449573741,
                      "user_id": 1234,
                      "body": "smg322"
                    }
                  ],
                  "body": "msg32"
                }
              ],
              "body": "msg3"
            }
          ],
          "body": "msg2"
        }
      ],
      "id": 414599,
      "body": "msg1",
      "out": 0
    }
  ]
}

Every time it can be different count of forwarded messages. So, I want to store these messages into an object. I think I should create the message object which can contain a list of forwarded messages and that forwarded messages can have other messages inside. My message object is:

public class Message implements Serializable {
public long date = 0;
public long fromId;
public boolean readState;
public int userId;
public String body;
public ArrayList<FwdMessage> fwdMessages;

public Message(long date, long fromId, String body) {
    this.date = date;
    this.fromId = fromId;
    this.body = body;
}}

And also I have my Forwarded messages object:

public class FwdMessage implements Serializable {
public long date;
public long userId;
public String body;
public List<FwdMessage> fwdMessages;

public FwdMessage(String body) {
    this.body = body;
}

public void addFwd(FwdMessage fwdMessage) {
    this.fwdMessages.add(fwdMessage);
}}

Of course, I can put all messages like they should be thanks to this method:

static int m = 0;

    public static void fwd(JSONObject o) {
        System.out.println(o.getString("body"));
        if(o.has("fwd_messages")) {
            m++;
            JSONArray array = o.getJSONArray("fwd_messages");
            for (int i = 0; i < array.length(); i++) {
                for (int j = 0; j < m; j++) {
                    System.out.print("\t");
                }
                fwd(array.getJSONObject(i));
            }
            m--;
        }
    }

But how can I put all messages together as I want that they should be in hierarchy order(e.g. 1st fwd_msgs has two other fwd_messages which have another fwd_messages)? Please, help me and sorry of my English :)

Coder
  • 9
  • 1
  • 5
  • you cold use [gson](https://github.com/google/gson) to convert json into Java Objects. `Message message = gson.fromJson(jsonString, Message.class);` – Cir0X Dec 08 '15 at 13:19
  • Jackson 3rd party API also, sounds good. you can use it. – Vishal Gajera Dec 08 '15 at 13:20
  • this might be helpfull to u : http://stackoverflow.com/questions/11038553/serialize-java-object-with-gson – nafas Dec 08 '15 at 13:21

0 Answers0