【问题标题】:Android: Dynamically Get JSON Array Key Name From JSONAndroid:从 JSON 动态获取 JSON 数组键名
【发布时间】:2016-06-22 06:51:45
【问题描述】:

我有一个 json 链接,如果我们打开它,我会得到以下结果

{
"Status": "Success",

"All_Details": [{
    "Types": "0",
    "TotalPoints": "0",
    "ExpiringToday": 0
}],
"First": [{
    "id": "0",
    "ImagePath": "http://first.example.png"
}],
"Second": [{
    "id": "2",
    "ImagePath": "http://second.example.png"
}],
"Third": [{
    "id": "3",
    "ImagePath": "http://third.example.png"
}],

}

我需要的是,我想动态获取所有关键名称,如状态、All_details、First 等。

我还想获取 All_details 和 First Array 中的数据。 我使用了以下方法

@Override
        public void onResponse(JSONObject response) throws JSONException {
            VolleyLog.d(TAG, "Home Central OnResponse: " + response);

            String statusStr = response.getString("Status");
            Log.d(TAG, "Status: " + statusStr);

            if (statusStr.equalsIgnoreCase("Success")) {
                Iterator iterator = response.keys();
                while (iterator.hasNext()) {
                    String key = (String)iterator.next();
                }
            }
        }

我将所有键名存储在 String 键中。但是我无法打开获取 JSON 数组中的值,例如。我需要使用 String(Key) 获取第一个和第二个数组中的值。我该怎么做。???

【问题讨论】:

    标签: java android arrays json android-json


    【解决方案1】:

    首先,要获取键名,您可以轻松地遍历 JSONObject 本身as mentioned here

    Iterator<?> keys = response.keys();
    while( keys.hasNext() ) {
        String key = (String)keys.next();
        if ( response.get(key) instanceof JSONObject ) {
            System.out.println(key); // do whatever you want with it
        }
    }
    

    然后,获取数组的值:

        JSONArray arr = response.getJSONArray(key);
        JSONObject element;
        for(int i = 0; i < arr.length(); i++){
            element = arr.getJSONObject(i); // which for example will be Types,TotalPoints,ExpiringToday in the case of the first array(All_Details) 
        }
    

    【讨论】:

    • @IndependentDev 欢迎你,很高兴我能帮助你:)
    【解决方案2】:

    如果您想从response JSONObject 获取 JSON 数组,您可以使用JSONArray classJSONObject 有一个方法可以得到一个JSONArraygetJSONArray(String)。请记住在尝试此操作时抓住JSONException。比如没有key就会抛出这个异常。

    您的代码可能如下所示(仅 while 循环):

    while (iterator.hasNext()) {
        String key = (String)iterator.next();
        try {
            JSONArray array = response.getJSONArray(key);
            // do some stuff with the array content
        } catch(JSONException e) {
            // handle the exception.
        }
    }
    

    您可以使用JSONArray 的方法从数组中获取值(参见文档)

    【讨论】:

      【解决方案3】:

      一旦您使用已完成的操作提取了键,这样的操作将允许您在数组和单个字段上进行迭代。使用您将在此之前创建的键变量代替“类型”。

      JSONArray allDetails = response.getJsonArray("All_Details")
      
      for (int i = 0 ; i < allDetails.length(); i++) {
          JSONObject allDetail = allDetails.getJSONObject(i);
          allDetails.getString("Types");
      }
      

      【讨论】:

      • 我不需要这个方法。我想动态获取数组名称(例如:All_details)并使用它来动态获取该数组中的值。
      【解决方案4】:

      首先我想通知您,它不是有效的JSON。删除最后一个 逗号 (,) 使其有效。

      然后你可以像这里一样迭代

      JSONArray myKeys = response.names();
      

      【讨论】:

        【解决方案5】:

        试试这个

        Iterator keys = jsonObject.keys();
            while (keys.hasNext()) {
                try {
                    String dynamicKey = (String) keys.next();//Your dynamic key
                    JSONObject item = jsonObject.getJSONObject(dynamicKey);//Your json object for that dynamic key
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-04-21
          • 2019-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-09
          相关资源
          最近更新 更多