1

我有一个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 通过了验证,我完全不知道哪个令牌失败了在。

我一定是在facepalm类别中遗漏了一些东西,但我根本找不到它......

错误信息 SyntaxError: Unexpected token

4

1 回答 1

1

您的 JSON 数据是完全有效的,但您还必须确保您的 PHP 脚本只发送 JSON 数据而没有其他内容(通知、警告、错误等会破坏 JSON)。

要检查,请使用浏览器的开发工具、FireBug 等,并查看网络检查器选项卡以查看 PHP 发送的实际响应。必要时修复它们的错误。

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

于 2013-03-04T13:05:11.697 回答