【发布时间】: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 结构的人。