我是PHP的新手,我正在使用wordpress JSON api。我想删除JSON数组中的Key:value对。请帮忙
{
"status":"ok",
"post":{
"id":23,
"type":"post",
"slug":"head-ache",
"url":"http:\/\/xyz.com\/maruthuvam\/2015\/06\/17\/head-ache\/",
"status":"publish",
"title":"Head Ache",
"title_plain":"Head Ache",
"content":"<p>content<\/p>\n<p> <\/p>\n",
"excerpt":"<p>content <\/p>\n",
"date":"2015-06-17 19:35:47",
"modified":"2015-06-18 07:35:39",
"categories":[
{
"id":1,
"slug":"head",
"title":"Head",
"description":"http:\/\/xyz.com\/maruthuvam\/wp-content\/uploads\/2015\/06\/universa_-male_head_3d_model_01.jpg",
"parent":0,
"post_count":3
}
],
"tags":[
],
"author":{
"id":1,
"slug":"admin",
"name":"admin",
"first_name":"",
"last_name":"",
"nickname":"admin",
"url":"",
"description":""
},
"comments":[
],
"attachments":[
],
"comment_count":0,
"comment_status":"closed",
"custom_fields":{
}
},
"next_url":"http:\/\/xyz.com\/maruthuvam\/2015\/06\/17\/head- lice\/"
}
例如,我想删除“slug”:“head-ache”来自“post”和“post_count”:0,来自“categories”和“next-url”。请帮忙。
UPDATE ::
我在core.php中添加了代码并且无法正常工作。你能帮帮我吗?
public function get_post() {
global $json_api, $post;
$post = $json_api->introspector->get_current_post();
if ($post) {
$previous = get_adjacent_post(false, '', true);
$next = get_adjacent_post(false, '', false);
$response = array(
'post' => new JSON_API_Post($post)
);
if ($previous) {
$response['previous_url'] = get_permalink($previous->ID);
}
if ($next) {
$response['next_url'] = get_permalink($next->ID);
}
// parsing json
$arr = decode_json($response);
// removing the value
unset($arr['post']['slug']);
// and back to json
$response = json_encode($arr);
return $response;
} else {
$json_api->error("Not found.");
}
}
答案 0 :(得分:0)
你需要将它解析为普通数组,然后删除你想要的东西并将其编码回json:
// parsing json
$arr = decode_json($yourJson);
// removing the value
unset($arr['post']['slug']);
// and back to json
$editedJson = json_encode($arr);
答案 1 :(得分:0)
只需将其转换为PHP数组:
$jsonArray = json_decode($jsonString);
删除密钥
unset($jsonArray['post']['slug']);
转换回来:
$newJson = json_encode($jsonArray);
答案 2 :(得分:0)
$a= json_decode($data,true);
unset($a['post']['slug']);
unset($a['next_url']);
$count= count($a['post']['categories']);
for($i=0 ; $i < $count ; $i++){
unset($a['post']['categories'][$i]['post_count']);
}
echo json_encode($a);