如何使用Gson解析这个Json并得到我想要的字段?

时间:2014-07-02 03:03:50

标签: json gson

{
"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": [ ]
    }
]
}

有些孩子是空的,有些则不是,而有些孩子则含有更多孩子。我如何实际解析这个东西,得到我想要的东西。我对Json全新,我正在尝试使用Gson。我想要的是在嵌套的Json中获取具有特定类型的标记的值。非常感谢任何帮助和指示。

我尝试过使用com.google.gson.stream.JsonReader,但不能正常工作

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();

1 个答案:

答案 0 :(得分:1)

像这样递归地解析你的json:

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