-2
{
"ws_result": 
[
    {
        "token": "", 
    "norm_token": "",
        "len": "", 
        "type": "", 
        "pos": "", 
        "prop": "", 
        "stag": "", 
        "child": 
    [
            {
                "token": "", 
"norm_token":"",
                "len": "", 
                "type": "", 
                "pos": "", 
                "prop": "", 
                "stag": "", 
                "child": 
[
                    {
                        "token": "", 
                                  "norm_token":"",
                        "len": "", 
                        "type": "", 
                        "pos": "", 
                        "prop": "", 
                        "stag": "", 
                        "child": [ ]
                    }, 
                    {
                        "token": "", 
                                 "norm_token":"",
                        "len": "", 
                        "type": "", 
                        "pos": "", 
                        "prop": "", 
                        "stag": "", 
                        "child": [ ]
                    }                   
                ]
            }, 
            {
                "token": "", 
        "norm_token":"", 
                "len": "", 
                "type": "", 
                "pos": "", 
                "prop": "", 
                "stag": "", 
                "child": 
[
                    {
                        "token": "", 
"norm_token":"",
                        "len": "", 
                        "type": "", 
                        "pos": "", 
                        "prop": "", 
                        "stag": "", 
                        "child": [ ]
                    }
                ]
            }
        ]
    }, 
    {
        "token": "", 
            "norm_token":"",
        "len": "", 
        "type": "", 
        "pos": "", 
        "prop": "", 
        "stag": "2", 
        "child": [ ]
    }, 
    {
        "token": "", 
            "norm_token": "",
        "len": "", 
        "type": "", 
        "pos": "", 
        "prop": "", 
        "stag": "", 
        "child": [ ]
    }
]
}

Such that some children are empty some is not, and some children contain more children. How do I actually parse this thing and get what I want. I am totally new with Json, and I am trying to use Gson. What I want is to get a value of a token with specific type in the nested Json. Thanks a lot for any help and directions.

I tried use com.google.gson.stream.JsonReader, but ist not working

JsonReader jsonReader = new JsonReader(new StringReader(result));
      jsonReader.beginObject();
      while(jsonReader.hasNext()){
           String field = jsonReader.nextName();
            if (field.equals("type")){
                System.out.println(jsonReader.nextString());
            } else if (field.equals("token")){
                System.out.println(jsonReader.nextString());
            } else {
         jsonReader.skipValue();
        }


        }
        jsonReader.endObject();
  • I have tried the following JsonReader jsonReader = new JsonReader(new StringReader(result)); jsonReader.beginObject(); while(jsonReader.hasNext()){ String field = jsonReader.nextName(); if (field.equals("ws_result")){ System.out.println(jsonReader.nextString()); } else if (field.equals("granu_type")){ System.out.println(jsonReader.nextString()); } else { jsonReader.skipValue(); } } jsonReader.endObject(); – user3796043 Jul 02 '14 at 03:15
  • I just need some Ideas, and how to handle nested json, what are the ways. I tried gson.stream.jsonreader – user3796043 Jul 02 '14 at 03:17
  • post code in the question, also the json you posted is invalid. – Taylor Jul 02 '14 at 03:17
  • how is this Json invalid? – user3796043 Jul 02 '14 at 03:18
  • it's not closed properly. Also, have you checked for existing question/answers for this issue? You can't be the first person to need to parse nested json structures with gson. – Taylor Jul 02 '14 at 03:18
  • possible duplicate of [Parsing nested JSON data using GSON](http://stackoverflow.com/questions/19169754/parsing-nested-json-data-using-gson) – Taylor Jul 02 '14 at 03:19
  • you can use this to view the Json, it is closed properly: http://json.parser.online.fr/ Also, my Json is nested with children, and that is not nested Can you also stop voting down my question? I really need to get help – user3796043 Jul 02 '14 at 03:27

1 Answers1

1

Parse your json recursively like this:

http://snipplr.com/view/71742/java-reflection-and-recursive-json-deserializer-using-gson/

private void parse(JsonObject o, PackagingResponse r){
        Iterator<Entry<String, JsonElement>> i = o.entrySet().iterator();
        while(i.hasNext()){
            Entry<String, JsonElement> e = i.next();
            JsonElement el = e.getValue();
            if(el.isJsonObject())
                parse(el.getAsJsonObject(), r);
            //......
        }
    }
Ramanan
  • 1,000
  • 1
  • 7
  • 20
  • Appreciated, i will try this method and let you know how it goes tmr!! thanks a lot for your help and direction. – user3796043 Jul 02 '14 at 05:51