【问题标题】:How can I put JSON return in an array?如何将 JSON 返回放入数组中?
【发布时间】: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') 并获取每个数据?

标签: android json parsing


【解决方案1】:

不要使用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】:

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

    其实是一个json对象,不是数组。这样你就不能将 json 对象转换为 json 数组 Json数组和Json对象的区别:

    1. JSONArray 是有序的值序列。 JSONObject 是名称/值对的无序集合。
    2. JSONArray:它的外部文本形式是一个用方括号括起来的字符串,用逗号分隔值。 JSONObject:它的外部形式是一个用大括号包裹的字符串,名称和值之间用冒号隔开,值和名称之间用逗号隔开。

    【讨论】:

      【解决方案3】:

      请使用这个 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) {
              }
      

      【讨论】:

        【解决方案4】:

        如果你问到关于迭代 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");
         }
        

        【讨论】:

          猜你喜欢
          • 2020-03-17
          • 2018-09-05
          • 2015-09-11
          • 2011-03-03
          • 2022-07-11
          • 2021-08-22
          • 2021-07-18
          • 1970-01-01
          相关资源
          最近更新 更多