我正在使用 goggle 的搜索 api来获取主题 id,该 id 用于从主题 api 获取 JSON 响应。返回的响应如下所示
{
"id":"/m/01d5g",
"property":{
"/amusement_parks/ride_theme/rides":{...},
"/award/ranked_item/appears_in_ranked_lists":{...},
"/book/book_character/appears_in_book":{
"valuetype":"object",
"values":[
{
"text":"Inferno",
"lang":"en",
"id":"/m/0g5qs3",
"creator":"/user/duck1123",
"timestamp":"2010-02-11T04:00:59.000Z"
},
{
"text":"Batman: Year One",
"lang":"en",
"id":"/m/0hzz_1h",
"creator":"/user/anasay",
"timestamp":"2012-01-25T11:05:03.000Z"
},
{
"text":"Batman: The Dark Knight Returns",
"lang":"en",
"id":"/m/0hzz_sb",
"creator":"/user/anasay",
"timestamp":"2012-01-25T11:22:17.001Z"
},
{
"text":"Batman: Son of the Demon",
"lang":"en",
"id":"/m/071l77",
"creator":"/user/wikimapper",
"timestamp":"2013-07-11T15:20:32.000Z"
},
{
"text":"Joker",
"lang":"en",
"id":"/m/04zxvhs",
"creator":"/user/wikimapper",
"timestamp":"2013-07-11T16:58:37.000Z"
},
{
"text":"Arkham Asylum: A Serious House on Serious Earth",
"lang":"en",
"id":"/m/0b7hyw",
"creator":"/user/wikimapper",
"timestamp":"2013-07-11T19:26:54.000Z"
}
],
"count":6.0
},
"/book/book_subject/works":{...},
"/comic_books/comic_book_character/cover_appearances":{...},
...
}
}
我想破译这个,以便我可以获得相关信息,例如,“/book/book_character/appears_in_book” 本身是一个响应属性,我想要的唯一值是“text”和“id”,例如“text” :"蝙蝠侠:第一年"和"id":"/m/0hzz_1h"。由于响应没有固定的属性,并且可能会根据响应 id 而变化。我如何在 java 类中隐藏这个 JSON 响应,我可以将“/book/book_character/appears_in_book”存储为一个序列化类并包含值的集合,例如具有id和text以及作为类的名称变量的出现_in_book 。
我考虑过 GSON来做这件事。由于属性名称不是常量,我不能使用它将 JSON 转换为 Java 对象。目前我正在通过硬编码迭代每个属性并将它们填充到java变量中。
如果有人可以提供有效的方法,我将不胜感激。