我有一个包含String数组(选民列表)的JSON对象。
这是JSON数组
http://jsonviewer.stack.hu/#http://www.saveme.ie/api/savings/
我的逻辑就像
将数组转换为JSONObject。
将JSON中的数组分配给List,并检查列表中的值。
String singleSaving = "";
ArrayList upVoters = new ArrayList();
// Loop through all and get single ID
for (int i = 0; i < response.length(); i++) {
JSONObject obj = null;
try {
obj = response.getJSONObject(i);
_id2 = obj.getString("_id");
if (_id2.equals(_id)) {
singleSaving = _id2;
// Id like to assign the upVoters array to
// the upVoters ArrayList here
}
} catch (JSONException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
您只需迭代upVoters
JSONArray
并填写ArrayList
:
String singleSaving = "";
ArrayList upVoters = new ArrayList();
// Loop through all and get single ID
for (int i = 0; i < response.length(); i++) {
JSONObject obj = null;
try {
obj = response.getJSONObject(i);
_id2 = obj.getString("_id");
if (_id2.equals(_id)) {
singleSaving = _id2;
// Get upVoters list as JSONArray
JSONArray upVotersJsonArray = obj.getJSONArray("upVoters");
for(int j=0; j<upVotersJsonArray.length(); j++) {
// Fill ArrayList with data from JSON
upVoters.add(upVotersJsonArray.getString(j));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}