Java 如何从json数组中获取内部数组值
这是json响应:Java 如何从json数组中获取内部数组值,java,android,json,Java,Android,Json,这是json响应: [ { "currentvalue": [ { "id": "13", "current_value": "0,1", "create_date": "2015-06-15 06:12:14", "status": "0", "modify_date": "0000-00
[
{
"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"
}
}
]
如何从“目标”数组中获取指示符名称值:
您可以这样尝试。
对于(int i=0;i您可以这样做
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");
}
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();
}
试试看{
JSONArray _jArrayMain=新的JSONArray(“YOURJSONSTRING”);
对于(int i=0;i<_jArrayMain.length();i++){
JSONObject jObj=jArrayMain.getJSONObject(i);
JSONObject_jObjTarget=_jObj.getJSONObject(“目标”);
String _indicator_name=_jObjTarget.getString(“indicator_name”);
System.out.println(“指标名称:”+_指标名称);
}
}捕获(JSONException e){
//TODO自动生成的捕捉块
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");
}
}
JsonArray responseJson=[
{
“当前值”:[
{
“id”:“13”,
“当前_值”:“0,1”,
“创建日期”:“2015-06-15 06:12:14”,
“状态”:“0”,
“修改日期”:“0000-00-00:00:00:00”,
“输入时间”:“,
“指标id”:“13”,
“成员id”:“9”
}
],
“目标”:{
“通知时间”:“06:12:14”,
“id”:“13”,
“选定的健康天数”:{
“日”:“3”
},
“点”:“1”,
“指标名称”:“测试2”,
“创建日期”:“2015-06-15 06:12:14”,
“指标状态”:“0”,
“通知类型”:“0”,
“修改日期”:“2015-06-15 06:12:14”,
“指标度量”:“0”,
“目标值”:“0,1”,
“总分”:“0”,
“成员id”:“9”
}
},
{
“当前值”:[
{
“id”:“12”,
“当前_值”:“0,1”,
“创建日期”:“2015-06-15 04:56:22”,
“状态”:“0”,
“修改日期”:“0000-00-00:00:00:00”,
“输入时间”:“,
“指标id”:“12”,
“成员id”:“9”
}
],
“目标”:{
“通知时间”:“02:02:00”,
“id”:“12”,
“选定的健康天数”:{
“日期”:”
},
“点”:“1”,
“指标名称”:“Ashish”,
“创建日期”:“2015-06-15 04:56:22”,
“指标状态”:“0”,
“通知类型”:“0”,
“修改日期”:“2015-06-15 04:56:22”,
“指标度量”:“0”,
“目标值”:“0,1”,
“总分”:“0”,
“成员id”:“9”
}
},
{
“当前值”:[
{
“id”:“11”,
“当前_值”:“0,1”,
“创建日期”:“2015-06-12 13:58:09”,
“状态”:“0”,
“修改日期”:“0000-00-00:00:00:00”,
“输入时间”:“,
“指标id”:“11”,
“成员id”:“9”
}
],
“目标”:{
“通知时间”:“02:02:00”,
“id”:“11”,
“选定的健康天数”:{
“日”:“1,2,3,4,5,6,7”
},
“点”:“123”,
“指标名称”:“A”,
“创建日期”:“2015-06-12 13:58:09”,
“指标状态”:“0”,
“通知类型”:“0”,
“修改日期”:“2015-06-12 13:58:09”,
“指标度量”:“0”,
“目标值”:“0,1”,
“总分”:“0”,
“成员id”:“9”
}
}];
对于(int i=0;i)我向我们展示了您所做的尝试。不鼓励简单地发布问题。请查看此答案
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");
}
}