PHP,JSON解码中的无效字符 [英] PHP, Invalid characters in JSON decode

查看:95
本文介绍了PHP,JSON解码中的无效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在让json_decode处理我收到的特定字符串时遇到麻烦.

我将其范围缩小到了这一行:

"systemNotes [6]":"2013年1月9日,下午12:52-测试名称-帐单邮寄地址2已从更改为送货姓氏:送货姓氏:电子邮件地址:送货地址:送货地址2:送货城市:邮递区号/邮寄:运送国家/地区:运送国家/地区:电话:帐单名:帐单姓氏:帐单地址:帐单地址2:帐单C"

从该问题复制json时,该问题无法重现-但原始json的代表性代码段在此处: http: //codepad.org/ZzrC7rqQ -将其放入 jsonlint.com 即可:

Parse error on line 3:
...  "systemNotes[6]": "January 09, 2013 12
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

此字符串有什么问题,以致于它是无效的json?

编辑

我设法找到了确切的代码.

"systemNotes [6]":一月+ 09%2C + 2013 + 12%3A52 + PM +-+ First + Name +-+更改++计费+地址+ 2 + From ++ to + Shipping + First + Name %3A%09 +运费+姓氏+名称%3A%09 +电子邮件+地址%3A%09 +运费+地址%3A +%09 +运费+地址+ 2%3A +%09 +运费+城市%3A +%09 +运费+邮政编码%2F邮政%3A +%09 +运费+国家/地区%3A +%09 +运费+状态%3A +%09 +电话%3A +%09 +计费+名字+名字%3A +%09 +计费+姓氏+名字%3A +%09 + Billing + Address%3A +%09 + Billing + Address + 2%3A +%09 + Billing + C"

这似乎没问题,所以问题可能出在我执行parse_str时,这是我正在使用的代码:

$response = apiConnection($data);
parse_str($response, $parse);
$each = json_decode($parse['data']);
foreach($each as $key => $order){
   //do something
}

解决方案

问题是制表符在字符串内无效.

删除标签字符,如 http://codepad.org/8fnQphkS 并在jsonlint.com上使用您会看到它现在看到有效的json.

http://www.ietf上查看JSON规范. .org/rfc/rfc4627.txt?number = 4627 特别是 2.5 部分,其中,按名称将制表符当作是在字符串中必须转义的字符之一. /p>

这是一种剥离所有制表符和多个空格并将其替换为单个空格字符的方法:

$data = preg_replace('/[ ]{2,}|[\t]/', ' ', trim($data));

I'm having troubles getting json_decode to work on a specific string I am receiving.

I have narrowed it down to this line:

"systemNotes[6]": "January 09, 2013 12:52 PM - Test Name - Changed Billing Address 2 From to Shipping First Name: Shipping Last Name: Email Address: Shipping Address: Shipping Address 2: Shipping City: Shipping Zip/Postal: Shipping Country: Shipping State: Phone: Billing First Name: Billing Last Name: Billing Address: Billing Address 2: Billing C"

Copying the json from this question, the problem is not reproducible - but a representative snippet of the original json is here: http://codepad.org/ZzrC7rqQ - and putting that in jsonlint.com gives:

Parse error on line 3:
...  "systemNotes[6]": "January 09, 2013 12
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

What is wrong with this string such that it's invalid json?

EDIT

I managed to find the exact code coming across.

"systemNotes[6]":"January+09%2C+2013+12%3A52+PM+-+First+Name+-+Changed++Billing+Address+2+From++to+Shipping+First+Name%3A%09+Shipping+Last+Name%3A%09+Email+Address%3A%09+Shipping+Address%3A+%09+Shipping+Address+2%3A+%09+Shipping+City%3A+%09+Shipping+Zip%2FPostal%3A+%09+Shipping+Country%3A+%09+Shipping+State%3A+%09+Phone%3A+%09+Billing+First+Name%3A+%09+Billing+Last+Name%3A+%09+Billing+Address%3A+%09+Billing+Address+2%3A+%09+Billing+C"

This seems to be ok so maybe the problem is coming from when I do the parse_str, here's the code I am using:

$response = apiConnection($data);
parse_str($response, $parse);
$each = json_decode($parse['data']);
foreach($each as $key => $order){
   //do something
}

解决方案

The problem is that tab characters are not valid inside a string.

Removing the tab characters like here http://codepad.org/8fnQphkS and using that at jsonlint.com you will see it see now valid json.

Have a look at the specs for JSON at http://www.ietf.org/rfc/rfc4627.txt?number=4627 specially section 2.5 where the tab character is called out by name as one of the characters that must be escaped if inside a string.

EDIT:

Here is a way of stripping out all tabs and multiple spaces and replacing them with a single space character:

$data = preg_replace('/[ ]{2,}|[\t]/', ' ', trim($data));

这篇关于PHP,JSON解码中的无效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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