【问题标题】:How to Parse this Json using Gson and get the field I want?如何使用 Gson 解析这个 Json 并获得我想要的字段?
【发布时间】:2014-07-02 03:03:50
【问题描述】:
{
"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();

【问题讨论】:

  • 我试过下面的 JsonReader jsonReader = new JsonReader(new StringReader(result)); jsonReader.beginObject(); while(jsonReader.hasNext()){ 字符串字段 = jsonReader.nextName(); if (field.equals("ws_result")){ System.out.println(jsonReader.nextString()); } else if (field.equals("granu_type")){ System.out.println(jsonReader.nextString()); } 其他 { jsonReader.skipValue(); } } jsonReader.endObject();
  • 我只是需要一些想法,以及如何处理嵌套的json,有什么方法。我试过 gson.stream.jsonreader
  • 问题中的post code,你发布的json也是无效的。
  • 这个Json怎么无效?
  • 它没有正确关闭。另外,您是否检查过此问题的现有问题/答案?你不可能是第一个需要用 gson 解析嵌套 json 结构的人。

标签: json gson


【解决方案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);
            //......
        }
    }

【讨论】:

  • 感激,我会试试这个方法,让你知道它是怎么回事tmr!非常感谢您的帮助和指导。
猜你喜欢
  • 1970-01-01
  • 2012-06-30
  • 2019-08-19
  • 1970-01-01
  • 2011-08-13
  • 1970-01-01
  • 2015-10-14
  • 2012-11-18
  • 2019-04-11
相关资源
最近更新 更多