如何从 JSON 中删除不需要的嵌套键
How to remove the unwanted nested keys from JSON
这是我的 json:
{
"all_counts_reports":{
"26":{
"name":"kumar",
"date":"2017-04-27",
"trips_per_day":"2",
"cash_trips":"0",
"credit_trips":"1",
"compliment_trips":"1"
},
"28":{
"name":"kumar",
"date":"2017-04-29",
"trips_per_day":"1",
"cash_trips":"1",
"credit_trips":"0",
"compliment_trips":"0"
}
}
}
我想使用 PHP.
删除二级密钥(例如 "26:"
和 "28":
)
换句话说,我想用零索引数字键替换双引号数字键。
我怎样才能让它看起来像这样:
{"all_counts_reports":
[
{"name":"kumar",
"date":"2017-04-27",
"trips_per_day":"2",
"cash_trips":"0",
"credit_trips":"1",
"compliment_trips":"1"
},
{"name":"kumar",
"date":"2017-04-29",
"trips_per_day":"1",
"cash_trips":"1",
"credit_trips":"0",
"compliment_trips":"0"
}
]
}
$array = json_decode($your_json_string,true);
print_r($array);
$result = array();
foreach($array['all_counts_reports'] as $rep){
$result['all_counts_reports'][] = array(
"name"=>$rep['name'],
"date"=>$rep['date'],
"trips_per_day"=>$rep['trips_per_day'],
"cash_trips"=>$rep['cash_trips'],
"credit_trips"=>$rep['credit_trips'],
"compliment_trips"=>$rep['compliment_trips'],
);
}
$result_json = json_encode($result);
echo $result_json;
可能有更好的解决方案,但我现在想到的是这个
不清楚你在找什么,如果你只是想删除而不想保持原样 json 那么你可以像这个例子一样做。但是,如果您不希望将 all_counts_reports
视为数组 []
,那么此示例将无济于事。
然后简单地传递给 android 然后上面的就可以了。
这里是 demo.
代码:
// declare $json
$array=json_decode($json,true); // decode as array
// overwrite subarray with zero-indexed keys while preserving subarray values
$array['all_counts_reports']=array_values($array['all_counts_reports']);
var_export(json_encode($array)); // return to json
输入:
$json='{
"all_counts_reports":{
"26":{
"name":"kumar",
"date":"2017-04-27",
"trips_per_day":"2",
"cash_trips":"0",
"credit_trips":"1",
"compliment_trips":"1"
},
"28":{
"name":"kumar",
"date":"2017-04-29",
"trips_per_day":"1",
"cash_trips":"1",
"credit_trips":"0",
"compliment_trips":"0"
}
}
}';
输出:
'{"all_counts_reports":
[
{"name":"kumar",
"date":"2017-04-27",
"trips_per_day":"2",
"cash_trips":"0",
"credit_trips":"1",
"compliment_trips":"1"
},
{"name":"kumar",
"date":"2017-04-29",
"trips_per_day":"1",
"cash_trips":"1",
"credit_trips":"0",
"compliment_trips":"0"
}
]
}'
在您的 javascript 中,使用 JSON.parse()
去除环绕的单引号:
var str='{"all_counts_reports":[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]}';
var json=JSON.parse(str);
console.log(json);
并且因为我们是 运行 假设,如果您还想删除 all_counts_reports
键,您可以使用这个单行代码:
代码:
$new_json=json_encode(array_values(current(json_decode($json,true))));
输出:
'[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]'
这是我的 json:
{
"all_counts_reports":{
"26":{
"name":"kumar",
"date":"2017-04-27",
"trips_per_day":"2",
"cash_trips":"0",
"credit_trips":"1",
"compliment_trips":"1"
},
"28":{
"name":"kumar",
"date":"2017-04-29",
"trips_per_day":"1",
"cash_trips":"1",
"credit_trips":"0",
"compliment_trips":"0"
}
}
}
我想使用 PHP.
删除二级密钥(例如"26:"
和 "28":
)
换句话说,我想用零索引数字键替换双引号数字键。
我怎样才能让它看起来像这样:
{"all_counts_reports":
[
{"name":"kumar",
"date":"2017-04-27",
"trips_per_day":"2",
"cash_trips":"0",
"credit_trips":"1",
"compliment_trips":"1"
},
{"name":"kumar",
"date":"2017-04-29",
"trips_per_day":"1",
"cash_trips":"1",
"credit_trips":"0",
"compliment_trips":"0"
}
]
}
$array = json_decode($your_json_string,true);
print_r($array);
$result = array();
foreach($array['all_counts_reports'] as $rep){
$result['all_counts_reports'][] = array(
"name"=>$rep['name'],
"date"=>$rep['date'],
"trips_per_day"=>$rep['trips_per_day'],
"cash_trips"=>$rep['cash_trips'],
"credit_trips"=>$rep['credit_trips'],
"compliment_trips"=>$rep['compliment_trips'],
);
}
$result_json = json_encode($result);
echo $result_json;
可能有更好的解决方案,但我现在想到的是这个
不清楚你在找什么,如果你只是想删除而不想保持原样 json 那么你可以像这个例子一样做。但是,如果您不希望将 all_counts_reports
视为数组 []
,那么此示例将无济于事。
然后简单地传递给 android 然后上面的就可以了。
这里是 demo.
代码:
// declare $json
$array=json_decode($json,true); // decode as array
// overwrite subarray with zero-indexed keys while preserving subarray values
$array['all_counts_reports']=array_values($array['all_counts_reports']);
var_export(json_encode($array)); // return to json
输入:
$json='{
"all_counts_reports":{
"26":{
"name":"kumar",
"date":"2017-04-27",
"trips_per_day":"2",
"cash_trips":"0",
"credit_trips":"1",
"compliment_trips":"1"
},
"28":{
"name":"kumar",
"date":"2017-04-29",
"trips_per_day":"1",
"cash_trips":"1",
"credit_trips":"0",
"compliment_trips":"0"
}
}
}';
输出:
'{"all_counts_reports":
[
{"name":"kumar",
"date":"2017-04-27",
"trips_per_day":"2",
"cash_trips":"0",
"credit_trips":"1",
"compliment_trips":"1"
},
{"name":"kumar",
"date":"2017-04-29",
"trips_per_day":"1",
"cash_trips":"1",
"credit_trips":"0",
"compliment_trips":"0"
}
]
}'
在您的 javascript 中,使用 JSON.parse()
去除环绕的单引号:
var str='{"all_counts_reports":[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]}';
var json=JSON.parse(str);
console.log(json);
并且因为我们是 运行 假设,如果您还想删除 all_counts_reports
键,您可以使用这个单行代码:
代码:
$new_json=json_encode(array_values(current(json_decode($json,true))));
输出:
'[{"name":"kumar","date":"2017-04-27","trips_per_day":"2","cash_trips":"0","credit_trips":"1","compliment_trips":"1"},{"name":"kumar","date":"2017-04-29","trips_per_day":"1","cash_trips":"1","credit_trips":"0","compliment_trips":"0"}]'