使用 Gson 解析 NYTimes Search API:如何反序列化嵌套的 JSON 元素?
我想解析以 JSON 格式给出的 NYT Search API 的响应。 JSON 字符串如下所示(摘录):
{"facets" :
{"des_facet" :
[
{"count" : 745 , "term" : "POLITICS AND GOVERNMENT"} ,
{"count" : 702 , "term" : "UNITED STATES INTERNATIONAL RELATIONS"}
],
"desk_facet" :
[
{"count" : 2251 , "term" : "Foreign Desk"} ,
{"count" : 242 , "term" : "Editorial Desk"}
]
}
}
在 Java 方面,我准备了以下对象层次结构:
public class Container {
Facet facets;
}
public class Facet {
Collection<Elements> des_facet;
Collection<Elements> desk_facet;
}
public class Elements {
private int count;
private String term;
}
...这显然不起作用。我是 JSON 新手。因此,元素的嵌套结构很混乱。
感谢您的帮助!
I would like to parse a response from the NYT Search API given in JSON format. The JSON string looks as follows (excerpt):
{"facets" :
{"des_facet" :
[
{"count" : 745 , "term" : "POLITICS AND GOVERNMENT"} ,
{"count" : 702 , "term" : "UNITED STATES INTERNATIONAL RELATIONS"}
],
"desk_facet" :
[
{"count" : 2251 , "term" : "Foreign Desk"} ,
{"count" : 242 , "term" : "Editorial Desk"}
]
}
}
On Java side, i prepared the following object hierarchy:
public class Container {
Facet facets;
}
public class Facet {
Collection<Elements> des_facet;
Collection<Elements> desk_facet;
}
public class Elements {
private int count;
private String term;
}
... which is obviously not working. I am new to JSON. Therefore, the nested structure of elements is confusing.
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
定义的类结构与示例 JSON 完美匹配,并且反序列化对我来说没有错误。
The defined class structure matches the example JSON perfectly well, and deserializes without error for me.