2013-03-04 15 views
1

我有一個的magento店,其輸出以下JSON的鏈接(請忽略用於測試目的的假值):由PHP意外的令牌,但是哪個令牌?

的jsfiddlehttp://jsfiddle.net/ZkZ4D/

非漂亮的格式化,輸出

[[{"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。

當我使用它int函數JSON.parse或jQuery.parseJSON我得到一個未被接受的令牌錯誤,但它並沒有說明哪個令牌或其中,並且因爲我的JSON通過了這個valdation我完全處於虧損狀態哪個令牌失敗。

我必須失去了一些東西在捂臉的類別,但我根本無法找到它...

錯誤信息 SyntaxError: Unexpected token

+0

您的JSON有效。問題在於別處。使用瀏覽器的開發人員工具> net inspector來查看您的服務器是否與JSON一起發佈PHP錯誤/警告/等。 – 2013-03-04 11:30:06

+1

由於顯而易見的原因,您的小提琴出現錯誤。你在字符串中有一個'\ n',它變成了一個字符串裏的一個字面的新行,這個字符串被破壞了JavaScript(未終止的字符串文字),因此破壞了JSON。 – 2013-03-04 11:47:09

+0

@SalmanA你是救世主!這就是我遇到的問題! 你可以將它發佈在答案中,以便我可以接受它嗎? – Tschallacka 2013-03-04 12:57:20

回答

1

您JSON數據是完全有效的,但你必須還要確保你的PHP腳本只發送JSON數據而沒有其他東西(注意,警告,錯誤等會打破JSON)。

要檢查,請使用瀏覽器的開發工具,FireBug等,然後查看網絡檢查器選項卡,查看PHP發送的實際響應。如有必要修復它們的錯誤。

至於你的小提琴:JSON數據不能在JavaScript字符串中原樣使用。至少您必須避開反斜槓(例如,JSON "Hello\nWorld"應該變爲'"Hello\\nWorld"')。