使用 json_encode 进行奇怪的 JSON 编码 [英] Weird JSON encoding using json_encode

查看:21
本文介绍了使用 json_encode 进行奇怪的 JSON 编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WordPress 和 JSON API 插件 (http://wordpress.org/extend/plugins/json-api/) 来生成对我们其他站点的响应.

I'm using WordPress together with the JSON API Plugin (http://wordpress.org/extend/plugins/json-api/) to generate responses to our other site.

当我传递以下数组 http 时,我遇到了一个非常奇怪的问题(我们使用的是 PHP 5.3.6)://pastebin.com/xdfYjrvK 到 json_encode() 它给了我这个(带有 json 内容类型):http://pastebin.com/T61XGPP5

I've hit a really weird problem (we're using PHP 5.3.6), when I pass the following array http://pastebin.com/xdfYjrvK to json_encode() it gives me this (with json content-type): http://pastebin.com/T61XGPP5

所以开头的废话,在上面的例子中,它是 2609,最后是 0,它根据响应的大小而变化,更多的内容 -> 更高的十六进制数.它也只有在响应量足够高"时才会出现,因此它适用于小响应.

So the crap in the beginning, in the above example it's 2609 and 0 in the end, it changes depending on the size of the response, more content -> higher hex number. It also appears only when the amount of response is "high enough", so it works on small responses.

一开始我以为是插件,但它可以在本地运行(在两台不同的 Mac OS X 机器上),我们已经将 VPS(Debian、Apache、Nginx、PHP)上的所有软件包更新到最新版本.

First I thought it was the plugin, but it works locally (on two different machines Mac OS X) and we've updated all packages on the VPS (Debian, Apache, Nginx, PHP) to the latest versions.

仅在发送内容类型时显示,而不是在以纯文本而不是 application/json 输出 $result 时显示:

It's only displayed when sending the content-type, not when outputting the $result with plain text instead of application/json:

$charset = get_option('blog_charset');
if (!headers_sent()) {
  header('HTTP/1.1 200 OK', true);
  header("Content-Type: application/json; charset=$charset", true);
}

echo $result;

$charset 设置为 utf-8.

$charset is set to utf-8.

Google chrome 控制台说:资源被解释为文档,但使用 MIME 类型 application/json 传输."

The Google chrome console says: "Resource interpreted as Document but transferred with MIME type application/json."

那么,有人知道这里发生了什么吗?

So, does anyone have a clue whats happening here?

推荐答案

这看起来像分块编码 (http://en.wikipedia.org/wiki/Chunked_transfer_encoding).请务必检查您的标头是否在响应中正确设置了 Content-Length,以确保您没有强制网络服务器使用 CTE.

This looks like chunked encoding (http://en.wikipedia.org/wiki/Chunked_transfer_encoding). Make sure to check that your headers are setting the Content-Length properly in the response to make sure you aren't forcing the web server to use CTE.

这篇关于使用 json_encode 进行奇怪的 JSON 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆