循环并将JSON对象转换为数组 [英] Looping and converting JSON Object to Array
本文介绍了循环并将JSON对象转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Java和Android编程的新手,我看过很多教程,但是我对如何循环遍历JSONObject并将其设置为类一无所知.
I'm new to Java and programming for Android and I've seen a lot of tutorials but I am kinda clueless atm on how to loop through a JSONObject and set it to my class.
JSON数据示例: http://sickbeard.com/api/#history
Example of JSON data: http://sickbeard.com/api/#history
我上过的课:
public Episode(JSONObject obj) {
try {
this.id = Integer.parseInt(obj.getString("episode").toString());
this.tvId = Integer.parseInt(obj.getString("tvdbid").toString());
this.resource = obj.getString("resource").toString();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
我到这为止...
ArrayList<Episode> episodeList = new ArrayList<Episode>();
JSONObject data = new JSONObject();
for(int i = 0; i < 2; i++) {
try {
data = response.getJSONObject("data");
episodeList.add(new Episode(data));
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
// for each entry create new episode :)
} else {
return null;
}
推荐答案
找到了:)
try {
response = new JSONObject(con.query("history", parameters));
JSONArray data = response.getJSONArray("data");
for(int i = 0; i < data.length(); i++) {
try {
episodeList.add(new Episode((JSONObject) data.get(i)));
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
这篇关于循环并将JSON对象转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文