2012-07-11 27 views
0

JSON數據,這是我的JSON格式無法檢索從給定的格式

({"message":{"success":true,"result":[{"lead_no":"LEA13","lastname":"Developer","firstname":"PHP","company":"Dummies","email":"[email protected]","id":"10x132"},{"lead_no":"LEA14","lastname":"Venu","firstname":"Yatagiri","company":"Rsalesarm","email":"[email protected]","id":"10x133"},{"lead_no":"LEA4","lastname":"Jones","firstname":"Barbara","company":"Vtigercrm inc","email":"[email protected]","id":"10x35"},{"lead_no":"LEA1","lastname":"Smith","firstname":"Mary","company":"Vtiger","email":"[email protected]","id":"10x32"}]}}) 

我將嘗試使用下面的代碼片段中檢索到整個JSON結果值

if (xmlHttp.readyState==4) 
    { 
     alert(xmlHttp.status); 
     if(xmlHttp.status==200) 
     { 

     alert("hi"); 
     var jsondata=eval("("+xmlHttp.responseText+")") //retrieve result as an JavaScript object 
     jsonOutput=jsondata.result; 
     alert(jsonOutput); 

     InitializeLeadStorage() 

     } 
    } 

alert (hi)顯示,但alert(jsonOutput);是不確定的,請幫助我,如果你能發現任何錯誤

回答

0
jsonOutput = jsondata.message.result; 

result住在message - 它不是JSON中的頂級項目。有了這樣的事情,console.log() JSON,你可以檢查你想要的位的路徑。

而且

  • 您的變量是全球
  • 有分析您的JSON的更好的方法。如果您不關心舊IE,則可以使用ECMA5 JSON.parse(),否則使用jQuery或另一個第三方實用程序來處理此問題
+0

jsonOutput = jsondata.message.result; 我用過這個,但仍然困擾着我,我沒有找到任何改變 – kavinhuh 2012-07-11 11:35:18

+0

你是什麼意思改變? 'jsondata.message.result'是否保持你期望的值? – Utkanos 2012-07-11 11:36:45

+0

實際上它擁有一個數組,我通過alert(jsonOutput)檢查了它;使用此警報,它顯示未定義的 – kavinhuh 2012-07-11 11:37:36