简体   繁体   中英

How to get inner array values from json-array

This is the json response:

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

How to get indicator_name values from "target" array:

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

you can try like this.

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 is not an array, it's an JSONObject in fact. this site may help to make the json data looks more clear.

You can do something like this.

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

You can use below code snippet.

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

In your example,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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