我把它缩小到这条线:
“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”
从这个问题中复制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]”:”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”
这似乎没问题所以也许问题来自于我做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.org/rfc/rfc4627.txt?number=4627特别是第2.5节中JSON的规范,其中制表符按名称调出,作为字符串中必须转义的字符之一.
编辑:
这是一种剥离所有选项卡和多个空格并用单个空格字符替换它们的方法:
$data = preg_replace('/[ ]{2,}|[\t]/',' ',trim($data));