我是一名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"}
将其放入数组时出错。
答案 0 :(得分:0)
如果您询问有关在json对象上进行迭代的问题,可以尝试一下
JSONObject jObject = new JSONObject(jsonStr);
JSONObject menu = jObject.getJSONObject("account_data");
Map<String,String> map = new HashMap<String,String>();
Iterator iter = menu.keys();
while(iter.hasNext()){
String key = (String)iter.next();
String value = menu.getString(key);
map.put(key,value);
}
所以现在您将数据作为键和值对
如果您有此响应的json数组,则可以执行以下操作
JSONObject root = new JSONObject("your root");
JSONArray resultArray = root.getJSONArray("your array key");
for (int i = 0; i < resultArray.length(); i++) {
// here to get json object one by one and access every item into it
JSONObject resultObject = resultArray.getJSONObject(i);
posterPath = resultObject.getString("key");
title = resultObject.getString("key");
releaseDate = resultObject.getString("key");
description = resultObject.getString("key");
voteAverage = resultObject.getDouble("key");
}
答案 1 :(得分:0)
请尝试使用JSONArray
,而不要使用JSONObject
。
String[] array = {json.get("sin"), json.get("account_name"), json.get("address"), json.get("status"), json.get("due_date"), json.get("total_amount") }
答案 2 :(得分:0)
{“ account_data”:{“ sin”:“ 200111-102”,“ account_name”:“ LUMABAN,CRISTOM”,“地址”:“ 352 MABINI ST。,, SABANG,Baliwag”,“状态”:“ A“,”到期日期“:” 2019-04-23“,”总金额“:” 491.00“},”代码“:1101,”邮件“:”已获取帐户信息“}
实际上,它是一个json对象,而不是数组。这样就不能将json对象转换为json数组 Json Array和Json Object之间的区别:
答案 3 :(得分:0)
请使用此json parshing
try {
JSONObject jsonObject = new JSONObject(jsonStr);
JSONObject accounts = jsonObject.getJSONObject("account_data");
sin = accounts.getString("sin");
account_name = accounts.getString("account_name");
address = accounts.getString("address");
status = accounts.getString("status");
due_date = accounts.getString("due_date");
total_amount = accounts.getDouble("total_amount");
sin_lbl.setText(a.getString("account_name"));
} catch (Exception e) {
}