[英]How to parse JSONObject with other JSONObjects inside it?
I have a question about getting jsonobjects out of a jsonobject. 我有一个关于从jsonobject中获取jsonobjects的问题。
This is what im getting back: 这就是我回来的东西:
{
"data": {
"someid": {
"air_by_date": 0,
"cache": {
"banner": 1,
"poster": 1
},
"language": "en",
"network": "somenetwork",
"next_ep_airdate": "somedate",
"paused": 0,
"quality": "somequality",
"show_name": "somename",
"status": "Continuing",
"tvdbid": someid,
"tvrage_id": someid,
"tvrage_name": "Showname"
},
"someid": {
"air_by_date": 0,
"cache": {
"banner": 1,
"poster": 1
},
"language": "en",
"network": "somenetwork",
"next_ep_airdate": "",
"paused": 0,
"quality": "somequality",
"show_name": "somename",
"status": "Continuing",
"tvdbid": someid,
"tvrage_id": someid,
"tvrage_name": "somename"
},
But how am i supposed to create "ShowObjects" of them. 但是我应该如何创建它们的“ ShowObjects”。 I know how it works with JSONArrays but i have never done this kind of JSON before. 我知道它如何与JSONArrays一起使用,但是我以前从未做过这种JSON。
This is what i got untill now: 这是我直到现在得到的:
String json = download(url);
JSONObject result = new JSONObject(json);
JSONObject resultData = result.getJSONObject("data");
Copied from this answer : 从此答案复制:
Use the keys() iterator to iterate over all the properties, and call get() for each. 使用keys()迭代器迭代所有属性,并对每个属性调用get()。
Iterator<String> iter = json.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
Object value = json.get(key);
} catch (JSONException e) {
// Something went wrong!
}
}
Try using Jackson or Gson for these tasks. 尝试将Jackson或Gson用于这些任务。
With Jackson you would do something like this: 使用Jackson,您将执行以下操作:
class Response() {
public String message;
public String result;
public Map<String, Object> data;
}
ObjectMapper mapper = new ObjectMapper()
Resonse respone = mapper.readValue(json, Response.class)
or use a custom deserializer to read those values out. 或使用自定义反序列化器读取这些值。
I will give you an example how to fetch the objects and vars in your "data" json: 我将举一个示例,说明如何在“数据” json中获取对象和变量:
lets say the second "someid" is : "123". 可以说第二个“ someid”是:“ 123”。 We will fetch the second jsonObject now : 我们现在将获取第二个jsonObject:
JSONObject secondJsonSomeId = resultData.getJsonObject("123");
Now we will fetch the "banner" ( int =1 ) of the jsonObject named "cache" located in our secondJsonSomeId : 现在,我们将获取位于secondJsonSomeId中的名为“ cache”的jsonObject的“横幅”(int = 1):
int bannerInsecondJsonSomeId = secondJsonSomeId.getJsonObject("cache").getInt("banner");// bannerInsecondJsonSomeId == 1
the code is not compiled so there might be errors , ask if you have additional questions. 该代码未编译,因此可能存在错误,请询问是否还有其他问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.