[英]How to get inner array values from json-array
这是 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"
}
}
]
如何从“目标”数组中获取指标名称值:
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);
你可以这样试试。
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。 该站点可能有助于使 json 数据看起来更清晰。
你可以做这样的事情。
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();
}
您可以使用以下代码片段。
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);
}
在你的例子中,
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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.