3

How to parse below stream of Json objects? There are example for array parsing but not stream of json objects. Only the first object is different other than that every other object is similar. Its not array but stream of json objects.

[{
  "code": 200,
  "request_id": "52d868df5ada23e5f289320f",
  "ok": true,
  "payload_meta": {
    "original_size": 1837,
    "size": 1837
  }
},{
  "id": "4fb56d7f273fb7ebfe22783f",
  "duration": "6:49",
  "duration_seconds": 409,
  "size_bytes": 16396948
}{
  "id": "4fb56d7f273fb7ebfe227841",
  "duration": "3:42",
  "duration_seconds": 222,
  "size_bytes": 8904980
}{
  "id": "4fb56d7f273fb7ebfe227846",
  "duration": "4:06",
  "duration_seconds": 246,
  "size_bytes": 9843339
}]

And also how to notify after parsing one object successfully instead of waiting whole stream to complete.

giampaolo
  • 6,906
  • 5
  • 45
  • 73
Prakash S
  • 632
  • 6
  • 11
  • your json format is invalid, check it here http://jsonlint.com/ – Asif Bhutto May 09 '14 at 04:54
  • I just gave an example of JSon object stream. Format may not be correct. What I need is parsing a stream of objects which is not in the format of json array. – Prakash S May 09 '14 at 07:10
  • What do you have try so far? – giampaolo May 11 '14 at 21:01
  • @giampaolo I tired with retrofit api with Callback as List for which GSON failed paring also i tried Callback which also failed while paring Error was "Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2" – Prakash S May 12 '14 at 10:03
  • If you edit your question giving more information, you have more chances that someone (including me) can answer. Take a look at the FAQ. – giampaolo May 12 '14 at 10:10
  • @PrakashS: moreover if you check your JSON with http://json.parser.online.fr/ you'll find that is incorrect. Improve the quality of your answer if you want a quality feedback from SO. – giampaolo May 12 '14 at 19:52
  • @giampaolo Please check the formatted json data and question – Prakash S May 13 '14 at 07:02
  • You're missing two commas, otherwise the JSON looks (at first glance) to be valid. As to how to parse it, just parse it (after adding the missing commas). It's an array of 4 JSON objects, very easy to parse and very easy to comprehend. – Hot Licks May 14 '14 at 21:08

1 Answers1

0

I made no assumptions on the kind of object you are trying to deserialize. So I named Class1 and Class2 the two kind of objects and they have no relationship. I declared them as inner static classes to make compat the example, but you can move Class1 and Class2 to separate files. I paste you a ready to run code so that you can try it on your own.

package stackoverflow.questions.q23556772;

import java.util.*;

import com.google.gson.*;

public class Q23556772 {

    public static class Class1 {
        String code;
        String request_id;
        Boolean ok;
        HashMap<String, Integer> payload_meta;

        @Override
        public String toString() {
            return "Class1 [code=" + code + ", request_id=" + request_id + ", ok=" + ok + ", payload_meta=" + payload_meta + "]";
        }

    }

    public static class Class2 {
        String id;
        String duration;
        Integer duration_seconds;
        Integer size_bytes;

        @Override
        public String toString() {
            return "Class2 [id=" + id + ", duration=" + duration + ", duration_seconds=" + duration_seconds + ", size_bytes=" + size_bytes + "]";
        }

    }


    public static void main(String[] args) {
     String json = 
             "[{                                                     "+
             "  \"code\": 200,                                       "+
             "  \"request_id\": \"52d868df5ada23e5f289320f\",        "+
             "  \"ok\": true,                                        "+
             "  \"payload_meta\": {                                  "+
             "    \"original_size\": 1837,                           "+
             "    \"size\": 1837                                     "+
             "  }                                                    "+
             "},{                                                    "+
             "  \"id\": \"4fb56d7f273fb7ebfe22783f\",                "+
             "  \"duration\": \"6:49\",                              "+
             "  \"duration_seconds\": 409,                           "+
             "  \"size_bytes\": 16396948                             "+
             "},{                                                    "+
             "  \"id\": \"4fb56d7f273fb7ebfe227841\",                "+
             "  \"duration\": \"3:42\",                              "+
             "  \"duration_seconds\": 222,                           "+
             "  \"size_bytes\": 8904980                              "+
             "},{                                                    "+
             "  \"id\": \"4fb56d7f273fb7ebfe227846\",                "+
             "  \"duration\": \"4:06\",                              "+
             "  \"duration_seconds\": 246,                           "+
             "  \"size_bytes\": 9843339                              "+
             "}]                                                     ";



     ArrayList<Object> result = new ArrayList<>();

     Gson g = new Gson();

     JsonArray e = new JsonParser().parse(json).getAsJsonArray();

     for(int i = 0; i < e.size(); i++){
         JsonObject o = e.get(i).getAsJsonObject();
         if (o.get("code") != null)
             result.add(g.fromJson(o, Class1.class));
         else if (o.get("id") != null)
             result.add(g.fromJson(o, Class2.class));
         else result.add(g.fromJson(o, Object.class));
     }

     for(Object resultObject: result)
         System.out.println(resultObject.toString());


    }

}

As you can see, I use a combination of JsonParser and Gson methods. I use the parser to peek inside the "stream" and to decide what is the correct class to use, then I use standard Gson deserialization to do the work. This code could be adapted into a custom TypeAdapter, but probably it makes the code "complex" beyond your real needs.

Final note: I edited your JSON since it was incorrect.

giampaolo
  • 6,906
  • 5
  • 45
  • 73