Php 意外的令牌,但哪个令牌?

Php 意外的令牌,但哪个令牌?,php,json,Php,Json,我有一个与magento商店的链接,该商店输出以下json(出于测试目的,请忽略伪值): jsfiddle [ [ { "customer_address_id": "4", "created_at": "2013-01-14 10:49:59", "updated_at": "2013-01-14 10:49:59", "city": "abc town",

我有一个与magento商店的链接,该商店输出以下json(出于测试目的,请忽略伪值):

jsfiddle

[
    [
        {
            "customer_address_id": "4",
            "created_at": "2013-01-14 10:49:59",
            "updated_at": "2013-01-14 10:49:59",
            "city": "abc town",
            "country_id": "NL",
            "firstname": "john",
            "lastname": "doe",
            "postcode": "7091 eh",
            "street": "mwhahah 47\nmwhgahahahaha",
            "telephone": "31645494440",
            "is_default_billing": true,
            "is_default_shipping": true
        }
    ],
    [
        {
            "customer_address_id": "4",
            "created_at": "2013-01-14 10:49:59",
            "updated_at": "2013-01-14 10:49:59",
            "city": "abc town",
            "country_id": "NL",
            "firstname": "john",
            "lastname": "doe",
            "postcode": "7091 eh",
            "street": "mwhahah 47\nmwhgahahahaha",
            "telephone": "31645494440",
            "is_default_billing": true,
            "is_default_shipping": true
        }
    ]
]
非漂亮格式,由php输出

[[{"customer_address_id":"4","created_at":"2013-01-14 10:49:59","updated_at":"2013-01-14 10:49:59","city":"abc town","country_id":"NL","firstname":"john","lastname":"doe","postcode":"7091 eh","street":"mwhahah 47\nmwhgahahahaha","telephone":"31645494440","is_default_billing":true,"is_default_shipping":true}],[{"customer_address_id":"4","created_at":"2013-01-14 10:49:59","updated_at":"2013-01-14 10:49:59","city":"abc town","country_id":"NL","firstname":"john","lastname":"doe","postcode":"7091 eh","street":"mwhahah 47\nmwhgahahahaha","telephone":"31645494440","is_default_billing":true,"is_default_shipping":true}]]
适合人类阅读的漂亮格式

[
    [
        {
            "customer_address_id": "4",
            "created_at": "2013-01-14 10:49:59",
            "updated_at": "2013-01-14 10:49:59",
            "city": "abc town",
            "country_id": "NL",
            "firstname": "john",
            "lastname": "doe",
            "postcode": "7091 eh",
            "street": "mwhahah 47\nmwhgahahahaha",
            "telephone": "31645494440",
            "is_default_billing": true,
            "is_default_shipping": true
        }
    ],
    [
        {
            "customer_address_id": "4",
            "created_at": "2013-01-14 10:49:59",
            "updated_at": "2013-01-14 10:49:59",
            "city": "abc town",
            "country_id": "NL",
            "firstname": "john",
            "lastname": "doe",
            "postcode": "7091 eh",
            "street": "mwhahah 47\nmwhgahahahaha",
            "telephone": "31645494440",
            "is_default_billing": true,
            "is_default_shipping": true
        }
    ]
]
如何获取上述json

php代码

class ajax extends plantinaNLmagento
    {
    public function __construct()
        {
        parent::__construct();
        } 
    public function getCustomerAdressAjax()
        {
        $id = (int)$_GET['customerid'];
        $q = $this->db->query("SELECT * FROM `tbl_magento_users` WHERE `core_id`=:ID",array('ID'=>$id));
        $customeradresses = array();
        while($who = $q->fetchObject())
            {
            $x=$this->mage->call('customer_address.list',$who->magento_ID);
            array_push($customeradresses,$x); 
            array_push($customeradresses,$x);
            }
        header('Cache-Control: no-cache, must-revalidate');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
        header('Content-type: application/json');
        echo json_encode($customeradresses);
        }
    }
出于测试目的,我推了两次
$customeraddress

现在,如果我将生成的json粘贴到jsonlint或其他json验证器中,它都表示它是有效的json

当我在函数JSON.parse或jQuery.parseJSON中使用它时,我会得到一个未预期的令牌错误,但它没有说明哪个令牌或在哪里,而且由于我的JSON通过了值,我完全不知道它在哪个令牌失败

我肯定在facepalm的分类中遗漏了什么,但我就是找不到

错误消息
SyntaxError:Unexpected token

您的JSON数据是完全有效的,但您还必须确保您的PHP脚本只发送JSON数据,而不发送任何其他数据(注意、警告、错误等将破坏JSON)

要进行检查,请使用浏览器的开发工具FireBug等,并查看network inspector选项卡以查看PHP发送的实际响应。如有必要,修复这些错误


至于你的小提琴:JSON数据不能在JavaScript字符串中使用。至少您必须避开反斜杠(例如,JSON
“Hello\nWorld”
应该变成
“Hello\\nWorld”
)。

您的JSON数据是完全有效的,但您还必须确保您的PHP脚本只发送JSON数据,而不发送任何其他数据(注意,警告、错误等会破坏JSON)

要进行检查,请使用浏览器的开发工具FireBug等,并查看network inspector选项卡以查看PHP发送的实际响应。如有必要,修复这些错误


至于你的小提琴:JSON数据不能在JavaScript字符串中使用。至少您必须避开反斜杠(例如,JSON
“Hello\nWorld”
应该变成
“Hello\\nWorld”
)。

您的JSON是有效的。问题在别处。使用浏览器的developer tools>net inspector查看您的服务器是否与JSON一起发出PHP错误/警告等。并且您会因为明显的原因收到错误。字符串中有一个
\n
,它在一个字符串中变成了一个文字换行符,该字符串是断开的JavaScript(未终止的字符串文字),因此是断开的JSON。@SalmanA你是救世主!这就是我的问题!你能把它贴在我的回复里吗?你的JSON是有效的。问题在别处。使用浏览器的developer tools>net inspector查看您的服务器是否与JSON一起发出PHP错误/警告等。并且您会因为明显的原因收到错误。字符串中有一个
\n
,它在一个字符串中变成了一个文字换行符,该字符串是断开的JavaScript(未终止的字符串文字),因此是断开的JSON。@SalmanA你是救世主!这就是我的问题!你能把它贴在回信里让我接受吗?