2013-10-22 117 views
0

你好這裏是來自AJAX調用我的JSON響應:解碼JSON對象

[{"id":null,"period":null,"until":null,"agent_id":"15","agent_zlecajacy_id":"15","offer_id":null,"status":"1","tytul":"Pobranie ksi\u0105g","tresc":"Pobranie ksi\u0105g","data_aktualizacji":"2013-10-21","data_kontaktu":"2013-10-08 22:00:00","data_kontaktu_end":"0000-00-00 00:00:00","czas_minuty":"30","created":"2013-10-21","type":"todo","series":null,"offers":"","details":"","parent_id":"0","assignment":null,"color":null,"asigned_users":null,"e_type":null,"show":null}] 

如何獲得例如狀態是 '1', 我tryed $ .parseJSON(結果),但得到

SyntaxError: JSON.parse: unexpected character 

也許因爲有空?

這裏是更多的代碼

url: "/schedule/getDetails/?id="+event_id, 
     dataType: 'json', 
     async: false, 
     success : function(json) { 
      result = json.result; 
         console.log($.parseJSON(result)); 

和PHP(Zend公司)

$result = $model->getDetails($id); 
      $this->sendJSON($result); 
+7

你的反應似乎是一個JavaScript對象,而不是一個字符串。 '$ .parseJSON'接受一個字符串arg。我說的是,你不需要解析..只是從對象中讀取。嘗試提醒'result [0] .status' –

+1

你確定這是'result'的值嗎?我只是嘗試'JSON.parse(<您發佈的JSON>)',它工作正常。 –

+0

http://jsonlint.com似乎驗證您提供的JSON。當試圖找出錯誤實際發生的位置時,更多的代碼會讓人們更多地使用。 –

回答

0

你的JSON響應無非是對象多用單一元素的數組中。所以,你可以訪問你想要的屬性:

your_response[0].attribute_name 

例如,下面的代碼將提取您的AGENT_ID:

myVar= [{"id":null,"period":null,"until":null,"agent_id":"15","agent_zlecajacy_id":"15","offer_id":null,"status":"1","tytul":"Pobranie ksi\u0105g","tresc":"Pobranie ksi\u0105g","data_aktualizacji":"2013-10-21","data_kontaktu":"2013-10-08 22:00:00","data_kontaktu_end":"0000-00-00 00:00:00","czas_minuty":"30","created":"2013-10-21","type":"todo","series":null,"offers":"","details":"","parent_id":"0","assignment":null,"color":null,"asigned_users":null,"e_type":null,"show":null}] 

alert(myVar[0].agent_id]) 
1

你應該

var a = [{"id":null,"period":null,"until":null,"agent_id":"15","agent_zlecajacy_id":"15","offer_id":null,"status":"1","tytul":"Pobranie ksi\u0105g","tresc":"Pobranie ksi\u0105g","data_aktualizacji":"2013-10-21","data_kontaktu":"2013-10-08 22:00:00","data_kontaktu_end":"0000-00-00 00:00:00","czas_minuty":"30","created":"2013-10-21","type":"todo","series":null,"offers":"","details":"","parent_id":"0","assignment":null,"color":null,"asigned_users":null,"e_type":null,"show":null}]; 

console.log(a[0]); 

DEMO

enter image description here

這之後,您可以訪問元素,如波紋管

console.log(a[0].id); 
console.log(a[0].period);