如何在导出时从JSON文件中移除/排除html编码?

时间:2015-11-04 作者:user82981

我使用json创建了一个文件并将其导出。它使用它创建HTML标记。实际上,我不想让html部分出现在文件中。下面我分享了我当前文件的结构:HTML默认标记,如head、title等,下面是json alon。

<html xmlns="http://www.w3.org/1999/xhtml" class="wp-toolbar"  lang="en-US">
<!--<![en-->
[{
    "ID" : 13,
    "post_author" : "1",
    "post_date" : "2015-10-30 07:09:16",
    "post_date_gmt" : "2015-10-30 07:09:16",
    "post_content" : "Test",
    "post_title" : "Test",
    "post_excerpt" : "",
    "post_status" : "publish",
    "comment_status" : "closed",
    "ping_status" : "closed",
    "post_password" : "",
    "post_name" : "test",
    "to_ping" : "",
    "pinged" : "",
    "post_modified" : "2015-10-30 07:09:16",
    "post_modified_gmt" : "2015-10-30 07:09:16",
    "post_content_filtered" : "",
    "post_parent" : 0,
    "guid" : "url/?post_type=wpcp_pointer&#038;p=9",
    "menu_order" : 0,
    "post_type" : "wpcp_pointer",
    "post_mime_type" : "",
    "comment_count" : "0",
    "filter" : "raw"
}
]
但我想要的是:

[{
    "ID" : 13,
    "post_author" : "1",
    "post_date" : "2015-10-30 07:09:16",
    "post_date_gmt" : "2015-10-30 07:09:16",
    "post_content" : "Test",
    "post_title" : "Test",
    "post_excerpt" : "",
    "post_status" : "publish",
    "comment_status" : "closed",
    "ping_status" : "closed",
    "post_password" : "",
    "post_name" : "test",
    "to_ping" : "",
    "pinged" : "",
    "post_modified" : "2015-10-30 07:09:16",
    "post_modified_gmt" : "2015-10-30 07:09:16",
    "post_content_filtered" : "",
    "post_parent" : 0,
    "guid" : "url/?post_type=wpcp_pointer&#038;p=9",
    "menu_order" : 0,
    "post_type" : "wpcp_pointer",
    "post_mime_type" : "",
    "comment_count" : "0",
    "filter" : "raw"
}
]
只有JSON数组存在文件导出。请帮助我如何从json文件中删除html部分。以下是用于生成此代码的编码部分:

$json_file = json_encode($need_options); // Encode data into json data
$json_file = stripslashes($json_file);

echo $json_file;

header("Content-Type: text/json; charset=" . get_option( \'blog_charset\'));
header("Content-Disposition: attachment; filename=$json_name.json");
exit();
任何帮助都会得到很好的回报。

1 个回复
SO网友:s_ha_dum

您在描述中遗漏了一些内容,即不清楚如何/在何处加载代码。我猜您是在以某种方式通过模板文件推送东西,或者可能是通过后端插件文件推送东西,因为您看到了看起来像模板代码的东西。

在我看来,您应该使用AJAX API:

function my_grab_ajax_wpse_108874() {
  if (empty($_GET[\'need_options\'])) die;
  $need_options = $_GET[\'need_options\']; // ??????
  $need_options = get_post($need_options);

  $json_name = $need_options->post_name;

  $json_file = json_encode($need_options); // Encode data into json data
  $json_file = stripslashes($json_file);

  header("Content-Type: text/json; charset=" . get_option( \'blog_charset\'));
  header("Content-Disposition: attachment; filename=$json_name.json");

  echo $json_file;

  exit();
}
add_action(\'wp_ajax_my_grab_ajax\', \'my_grab_ajax_wpse_108874\');
add_action(\'wp_ajax_nopriv_my_grab_ajax\', \'my_grab_ajax_wpse_108874\');
然后使用http://example.com/wp-admin/admin-ajax.php?action=mygrab_ajax&need_options={some post ID}