简体   繁体   English

如何将JSON返回数组?

[英]How can I put JSON return in an array?

I'm a android beginner and I'm doing to access a JSON file in and it has an error. 我是一个Android初学者,我正在访问一个JSON文件,它有一个错误。 I have a problem in parsing this 我在解析这个问题时遇到了问题

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"));
    }

here is the JSON File 这是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"}

I have an error in putting it in array. 将它放入数组时出错。

if you asked about iterating on json object you could try this one 如果你问过关于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);
 }

so now you have your data into as pair of key and value 所以现在你把你的数据作为一对键和值

if you have a json array of this response you could do as following 如果你有一个这个响应的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");
 }

而不是使用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") }

{"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”:{“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”:“帐户信息已检索”}

Actually, it's a json object, not array. 实际上,它是一个json对象,而不是数组。 So that you can not convert json object to json array Difference between Json Array and Json Object: 这样你就无法将json对象转换为json数组Json Array和Json Object之间的区别:

  1. A JSONArray is an ordered sequence of values. JSONArray是一个有序的值序列。 A JSONObject is an unordered collection of name/value pairs. JSONObject是名称/值对的无序集合。
  2. JSONArray: Its external text form is a string wrapped in square brackets with commas separating the values. JSONArray:它的外部文本形式是一个用方括号括起来的字符串,用逗号分隔值。 JSONObject: Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. JSONObject:它的外部形式是一个用大括号括起来的字符串,名称和值之间有冒号,值和名称之间有逗号。

Please use this json parshing 请使用此json解析

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) {
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM