我有一个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