1

我是 PHP 新手,我正在研究 wordpress JSON api。我想删除 JSON 数组中的键:值对。请帮忙

{  
 "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>&nbsp;<\/p>\n",
  "excerpt":"<p>content &nbsp;<\/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\/"
  }

例如,我想从 "post" 和 "post_count":0 中删除 "slug":"head-ache",从 "categories" 和 "next-url" 中删除。请帮忙。

更新::

我已经在 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.");
}

}

4

3 回答 3

3

只需将其转换为 PHP 数组:

$jsonArray = json_decode($jsonString);

删除密钥

unset($jsonArray['post']['slug']);

并转换回来:

$newJson = json_encode($jsonArray);
于 2015-06-18T09:01:22.537 回答
0

您需要将其解析为普通数组,然后删除您想要的并将其编码回 json:

// parsing json
$arr = decode_json($yourJson);

// removing the value
unset($arr['post']['slug']);

// and back to json
$editedJson = json_encode($arr);
于 2015-06-18T09:00:48.977 回答
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);
于 2015-06-18T09:06:28.837 回答