java如何使用gson库解析JSONObject
我正在使用maven依赖项解析带有gson library
的JsonObject
但是我不能有一个包含键和值的地图
我尝试使用keyset方法,但它没有找到与值(空指针异常错误)的对应关系。JSONArray字段重新发送了我想要获取的数组,但是currentJsonObject.get("fields")
返回一个object
,我无法解析它来获取其他键值。
这是object的结果,我通过get("fields")
从返回的对象中得到它
总而言之,我想解析这个数组中的JSONArray fields
和retrieve
值键
JsonReader jsonReader = new JsonReader(new InputStreamReader(new FileInputStream("filename"), StandardCharsets.UTF_8));
jsonReader.beginArray();
Gson gson = new GsonBuilder().create();
Collection<String> list ;
Collection<String> keys ;
BasicDBObject map = new BasicDBObject();
while (jsonReader.hasNext()) {
JSONObject currentJsonObject = gson.fromJson(jsonReader,
JSONObject.class);
System.out.println(currentJsonObject.get("fields"));
}
jsonReader.close();
Json:
# 1 楼答案
下面的代码可能会帮助您: