【问题标题】:How to get inner array values from json-array如何从 json-array 获取内部数组值
【发布时间】:2015-06-16 05:14:15
【问题描述】:

这是 json 响应:

[
    {
        "currentvalue": [
            {
                "id": "13",
                "current_value": "0,1",
                "create_date": "2015-06-15 06:12:14",
                "status": "0",
                "modify_date": "0000-00-00 00:00:00",
                "entry_time": "",
                "indicator_id": "13",
                "member_id": "9"
            }
        ],
        "target": {
            "notification_time": "06:12:14",
            "id": "13",
            "health_selected_days": {
                "day": "3"
            },
            "point": "1",
            "indicator_name": "Test2",
            "create_date": "2015-06-15 06:12:14",
            "indicator_status": "0",
            "notification_type": "0",
            "modify_date": "2015-06-15 06:12:14",
            "indicator_measure": "0",
            "target_value": "0,1",
            "total_point": "0",
            "member_id": "9"
        }
    },
    {
        "currentvalue": [
            {
                "id": "12",
                "current_value": "0,1",
                "create_date": "2015-06-15 04:56:22",
                "status": "0",
                "modify_date": "0000-00-00 00:00:00",
                "entry_time": "",
                "indicator_id": "12",
                "member_id": "9"
            }
        ],
        "target": {
            "notification_time": "02:02:00",
            "id": "12",
            "health_selected_days": {
                "day": ""
            },
            "point": "1",
            "indicator_name": "Ashish",
            "create_date": "2015-06-15 04:56:22",
            "indicator_status": "0",
            "notification_type": "0",
            "modify_date": "2015-06-15 04:56:22",
            "indicator_measure": "0",
            "target_value": "0,1",
            "total_point": "0",
            "member_id": "9"
        }
    },
    {
        "currentvalue": [
            {
                "id": "11",
                "current_value": "0,1",
                "create_date": "2015-06-12 13:58:09",
                "status": "0",
                "modify_date": "0000-00-00 00:00:00",
                "entry_time": "",
                "indicator_id": "11",
                "member_id": "9"
            }
        ],
        "target": {
            "notification_time": "02:02:00",
            "id": "11",
            "health_selected_days": {
                "day": "1,2,3,4,5,6,7,"
            },
            "point": "123",
            "indicator_name": "A",
            "create_date": "2015-06-12 13:58:09",
            "indicator_status": "0",
            "notification_type": "0",
            "modify_date": "2015-06-12 13:58:09",
            "indicator_measure": "0",
            "target_value": "0,1",
            "total_point": "0",
            "member_id": "9"
        }
    }
]

如何从“目标”数组中获取指标名称值:

【问题讨论】:

标签: java android json


【解决方案1】:

你可以这样做。

try {
                    JSONArray _jArrayMain = new JSONArray("YOURJSONSTRING");
                    for (int i = 0; i < _jArrayMain .length(); i++) {
                        JSONObject _jObj = _jArrayMain.getJSONObject(i);
                        JSONObject   _jObjTarget = _jObj.getJSONObject("target");
                        String _indicator_name = _jObjTarget.getString("indicator_name");
                        System.out.println("Indicator Name : " + _indicator_name);
                    }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

【讨论】:

    【解决方案2】:
        JSONParser parser = new JSONParser();
    
        Object obj = parser.parse(new FileReader("your file path"));
    
        JSONObject jsonObject = (JSONObject) obj;
    
        String name = (String) jsonObject.get("indicator_name");
        System.out.println(name);
    

    你可以这样试试。

    【讨论】:

      【解决方案3】:
      for(int i=0; i<jsonArray.length(); i++)
      {
          JSONObject object = jsonArray.getJSONObject(i);
          JSONObject target = object.getJSONObject("target");
          String indicator_name = target.getString("indicator_name");
      }
      

      target 不是一个数组,它实际上是一个 JSONObject。 this site 可能有助于使 json 数据看起来更清晰。

      【讨论】:

        【解决方案4】:

        你可以使用下面的代码sn-p。

        final JSONArray jsonArray = new JSONArray("JSON_STRING");
        for (JSONObject jObject: jsoonArray) {
             final JSONObject   jsonTargetObject = jObject.optJSONObject("target");
             if(jsonTargetObject != NULL) {
             final String indicatorName = jsonTargetObject.optString("indicator_name", null);
             System.out.println("Indicator Name : " + indicatorName);
        }
        

        【讨论】:

          【解决方案5】:

          在你的例子中,

          JsonArray responseJson = [
          {
              "currentvalue": [
                  {
                      "id": "13",
                      "current_value": "0,1",
                      "create_date": "2015-06-15 06:12:14",
                      "status": "0",
                      "modify_date": "0000-00-00 00:00:00",
                      "entry_time": "",
                      "indicator_id": "13",
                      "member_id": "9"
                  }
              ],
              "target": {
                  "notification_time": "06:12:14",
                  "id": "13",
                  "health_selected_days": {
                      "day": "3"
                  },
                  "point": "1",
                  "indicator_name": "Test2",
                  "create_date": "2015-06-15 06:12:14",
                  "indicator_status": "0",
                  "notification_type": "0",
                  "modify_date": "2015-06-15 06:12:14",
                  "indicator_measure": "0",
                  "target_value": "0,1",
                  "total_point": "0",
                  "member_id": "9"
              }
          },
          {
              "currentvalue": [
                  {
                      "id": "12",
                      "current_value": "0,1",
                      "create_date": "2015-06-15 04:56:22",
                      "status": "0",
                      "modify_date": "0000-00-00 00:00:00",
                      "entry_time": "",
                      "indicator_id": "12",
                      "member_id": "9"
                  }
              ],
              "target": {
                  "notification_time": "02:02:00",
                  "id": "12",
                  "health_selected_days": {
                      "day": ""
                  },
                  "point": "1",
                  "indicator_name": "Ashish",
                  "create_date": "2015-06-15 04:56:22",
                  "indicator_status": "0",
                  "notification_type": "0",
                  "modify_date": "2015-06-15 04:56:22",
                  "indicator_measure": "0",
                  "target_value": "0,1",
                  "total_point": "0",
                  "member_id": "9"
              }
          },
          {
              "currentvalue": [
                  {
                      "id": "11",
                      "current_value": "0,1",
                      "create_date": "2015-06-12 13:58:09",
                      "status": "0",
                      "modify_date": "0000-00-00 00:00:00",
                      "entry_time": "",
                      "indicator_id": "11",
                      "member_id": "9"
                  }
              ],
              "target": {
                  "notification_time": "02:02:00",
                  "id": "11",
                  "health_selected_days": {
                      "day": "1,2,3,4,5,6,7,"
                  },
                  "point": "123",
                  "indicator_name": "A",
                  "create_date": "2015-06-12 13:58:09",
                  "indicator_status": "0",
                  "notification_type": "0",
                  "modify_date": "2015-06-12 13:58:09",
                  "indicator_measure": "0",
                  "target_value": "0,1",
                  "total_point": "0",
                  "member_id": "9"
              }
          }];
          
          for(int i=0;i<responseJson.length();i++)
          {
              JsonObject currentJsonObj = responseJson.optJsonObject(i);
              JsonArray currentJsonA = responseJson.optJsonArray("currentvalue");
              for(int j=0;j<currentJsonA.length();j++)
              {
                  JsonObject innerObj = currentJsonA.optJsonObject(j);
                  String id = innerObj.optString("id");
                  String currentValue = innerObj.optString("current_value");
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-10-08
            • 2022-01-20
            • 1970-01-01
            • 2023-01-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多