以下是我从API获得的回复。
[{
"claim1": [{
"claim_no": "1",
"claim_date": "14\/06\/2017 12:00:00 AM",
"month_code": "1",
"claim_amount": "8074"
}]
}, {
"billing1": [{
"claim_no": "141",
"month_code": "1",
"miti": "6",
"amount": "7374",
"paid": "7374",
"fee1": "0"
}]
}, {
"claim2": [{
"claim_no": "26",
"claim_date": "20\/06\/2017 12:00:00 AM",
"month_code": "2",
"claim_amount": "2424"
}]
}, {
"billing2": []
}, {
"claim3": [{
"claim_no": "40",
"claim_date": "13\/07\/2017 12:00:00 AM",
"month_code": "3",
"claim_amount": "2924"
}]
}, {
"billing3": [{
"claim_no": "724",
"month_code": "3",
"miti": "13",
"amount": "2924",
"paid": "2924",
"fee1": "0"
}]
}, {
"claim4": [{
"claim_no": "43",
"claim_date": "09\/08\/2017 12:00:00 AM",
"month_code": "4",
"claim_amount": "2424"
}]
}, {
"billing4": [{
"claim_no": "402",
"month_code": "4",
"miti": "20",
"amount": "5348",
"paid": "3124",
"fee1": "0"
}]
}]
我正在使用aQuery jsonarray类 我试图在JSON对象中获取它,但是出现了类似
的错误jsonarray无法转换为jsonobject
下面是我的代码 此代码显示此错误
“org.json.JSONException:claim_no没有值”
我想获得claim_no
的值aq.ajax("myapiurlhere", JSONArray.class, new AjaxCallback<JSONArray>(){
@Override
public void callback(String url, JSONArray array, AjaxStatus status) {
super.callback(url, array, status);
Log.i("response:", "response:" + url + array);
for (int i = 0; i < array.length(); i++) {
try {
JSONObject object = array.getJSONObject(i);
bill_no = object.getString("claim_no");
Log.i("response:","claim: "+claim_no+" bill: "+object);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
我也试过下面的代码,但是我收到了错误
“org.json.JSONArray类型的claim1无法转换为JSONObject”
aq.ajax("myapiurl", JSONArray.class, new AjaxCallback<JSONArray>(){
@Override
public void callback(String url, JSONArray array, AjaxStatus status) {
super.callback(url, array, status);
Log.i("response:", "response:" + url + array);
for (int i = 0; i < array.length(); i++) {
try {
int mycount = i+1;
JSONObject object = array.getJSONObject(i);
JSONObject claimobject = object.getJSONObject("claim"+mycount);
claim_no = claimobject.getString("claim_no");
Log.i("response:","claim: "+claim_no);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
答案 0 :(得分:2)
这是一个数组
"claim1": [
您正在获取对象
JSONObject claimobject = object.getJSONObject("claim"+mycount);
你需要先获得一个数组
JSONObject claimobject = object.getJSONArray("claim"+mycount).getJSONObject(0);
此外,您的API结构不合理,int mycount = i+1;
可能会因为您同时计算结算和声明元素而失败
试试这个
for (int i = 0; i < array.length(); i++) {
int mycount = (i/2)+1;
try {