json_en code创建一个畸形的JSON(有额外的隐藏字符) [英] json_encode creating a malformed JSON (with extra hidden character)

查看:146
本文介绍了json_en code创建一个畸形的JSON(有额外的隐藏字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PHP的 json_en code()返回一些数据,通过jQuery的 AJAX检索()

简体JS:

  $。阿贾克斯({
    数据类型:JSON,
    的contentType:应用/ JSON的,
    缓存:假的,
    键入:POST,
    网址:./api/_imgdown.php,
    错误:函数(jqXHR,textStatus,errorThrow){
        的console.log(jqXHR,textStatus,errorThrow);
    },
    成功:功能(数据,textStatus,jqXHR){
        的console.log(数据,textStatus,jqXHR);
    }
});
 

该PHP是:

 标题(内容类型:应用程序/ JSON;字符集= UTF-8);
//默认apiResponse
$ apiResponse = [
    状态=> 1,
    消息=> 成功,
    数据=>空值
]。
 

然后当PHP运行我的code,它通过将这些数据结束:

  $ apiResponse ['数据'] [] = [
    MID=> (INT)$中旬,
    card_type=> $ card_type,
    header_size=> (INT)$头['的Content-Length'],
    saved_size=> (INT)文件大小($ imgSavePath)
    saved_pa​​th=> $ imgSavePath
]。
//吐出JSON
回声json_en code($ apiResponse);
出口();
 

JSON的:

<$p$p><$c$c>{"status":1,"message":"success","data":[{"mid":340052,"card_type":"kakusei","header_size":48337,"saved_size":48337,"saved_path":"..\/card\/kakusei\/340052.png"}]}

起初,这似乎有效。我的阿贾克斯检索这个PHP总是与 parseerror (由此进入了错误部分结束阿贾克斯())。

如果您复制并粘贴此JSON在 http://jsonlint.com/ ,它说:意外令牌和 http://jsonformatter.curiousconcept.com/ 说是无效的。

我已经试过回声json_en code($ apiResponse,JSON_UNESCAPED_SLASHES); 即使逃脱斜线是好的 \ / ,但并没有区别。

但是,什么是不完全有效的吗?是不是PHP应该返回有效的JSON?

额外的信息:在Windows7测试,Chrome浏览器v28.XX,使用Apache的PHP 5.4.XX

问题我其实之前发布这个已经阅读:

  • <一个href="http://stackoverflow.com/questions/12500834/json-en$c$c-creating-malformed-json-data">json_en$c$c创建畸形的JSON数据?
  • <一个href="http://stackoverflow.com/a/15654848/684932">http://stackoverflow.com/a/15654848/684932
  • 约json_en code和AJAX数据类型:&QUOT ; JSON&QUOT;
  • <一个href="http://stackoverflow.com/questions/8518580/php-json-en$c$c-not-returning-proper-json-en$c$cd-string">php json_en code没有返回正确的json EN codeD字符串

更新:

这所以复制到JSONlint给出有效的JSON。所以,我调查远一点,发现PHP的是创造一个奇怪的隐藏角色,实际上是使JSON无效。见下图。我该如何解决呢?

解决方案

这可能是一个问题,一个BOM标志。尝试将文件保存为正常的UTF-8来代替。

I'm using PHP's json_encode() to return some data, retrieved by jQuery's ajax():

Simplified JS:

$.ajax({
    dataType: 'json',
    contentType: 'application/json',
    cache: false,
    type: 'POST',
    url: './api/_imgdown.php',
    error: function(jqXHR, textStatus, errorThrow) {
        console.log(jqXHR, textStatus, errorThrow);
    },
    success: function(data, textStatus, jqXHR) {
        console.log(data, textStatus, jqXHR);
    }
});

The PHP is:

header('Content-Type: application/json; charset=UTF-8');
//default apiResponse
$apiResponse = [
    "status" => 1,
    "message" => "success",
    "data" => null
];

Then when php runs my code, it ends by adding this data:

$apiResponse['data'][] = [
    "mid" => (int)$mid,
    "card_type" => $card_type,
    "header_size" => (int)$headers['Content-Length'],
    "saved_size" => (int)filesize($imgSavePath),
    "saved_path" => $imgSavePath
];
//spit out the JSON
echo json_encode($apiResponse);
exit();

The JSON:

{"status":1,"message":"success","data":[{"mid":340052,"card_type":"kakusei","header_size":48337,"saved_size":48337,"saved_path":"..\/card\/kakusei\/340052.png"}]}

At first it seems valid. My Ajax that retrieves this PHP always ends with parseerror (thus going into the error part of ajax()).

If you copy and paste this JSON on http://jsonlint.com/, it says "Unexpected token " and http://jsonformatter.curiousconcept.com/ says is not valid.

I've tried echo json_encode($apiResponse, JSON_UNESCAPED_SLASHES); even though escaping slashes is ok \/ but made no difference.

But what isn't exactly valid? Wasn't PHP supposed to return valid JSON?

Extra info: Testing on Windows7, Chrome v28.XX, using PHP 5.4.XX on Apache

Questions I've read before actually posting this one:


Update:

Copying from SO into JSONlint gives valid json. So I investigated a little further and noticed the PHP is creating a weird hidden character that is actually making json invalid. See below screenshot. How I fix that?

解决方案

It could be an issue with a BOM mark. Try to save the file as normal UTF-8 instead.

这篇关于json_en code创建一个畸形的JSON(有额外的隐藏字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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