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

时间:2013-03-04 09:44:42

标签: php json

我有一个magento商店的链接,它输出以下json(请忽略虚假值以进行测试):

jsfiddle http://jsfiddle.net/ZkZ4D/

非常漂亮的格式化,由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通过了valdation我完全不知所措令牌失败了。

我必须遗漏 facepalm 类别中的某些内容,但我根本找不到它......

错误消息 SyntaxError: Unexpected token

1 个答案:

答案 0 :(得分:1)

您的JSON数据完全有效,但您还必须确保您的PHP脚本仅发送JSON数据而不发送任何其他内容(通知,警告,错误等将破坏JSON)。

要检查,请使用浏览器的开发工具,FireBug等,然后查看网络检查器标签,查看PHP发送的实际响应。如有必要,请修复错误。

至于你的小提琴:JSON数据不能在JavaScript字符串中使用。至少你必须逃避反斜杠(例如JSON "Hello\nWorld"应该成为'"Hello\\nWorld"')。