【发布时间】:2019-04-12 01:27:29
【问题描述】:
我是一名 android 初学者,我正在访问一个 JSON 文件,但它有一个错误。我在解析时遇到问题
JSONObject jsonObject = new JSONObject(jsonStr);
JSONArray accounts = jsonObject.getJSONArray("account_data");
for(int i=0;i < accounts.length();i++){
JSONObject a = accounts.getJSONObject(i);
sin = a.getString("sin");
account_name = a.getString("account_name");
address = a.getString("address");
status = a.getString("status");
due_date = a.getString("due_date");
total_amount = a.getDouble("total_amount");
sin_lbl.setText(a.getString("account_name"));
}
这是 JSON 文件
{"account_data":{
"sin":"200111-102 ",
"account_name":"LUMABAN, CRISTOM ",
"address":"352 MABINI ST.,, SABANG, Baliwag ",
"status":"A ",
"due_date":"2019-04-23",
"total_amount":"491.00"
},"code":1101,"message":"Account Info Retrieved"}
我将它放入数组时出错。
【问题讨论】:
-
具体是什么错误?
-
account_data 不是 jsonArray 为什么要调用 getJsonArray()?你需要
getJsonObject("account_data") -
JSONObject cannot be converted to JSONArray -
如何循环 getJsonObject("account_data') 并获取每个数据?