2012-02-03 78 views
1

我被困在這個問題中,我調用了一個返回給我一個json響應的webService。從JSON.parse數組中獲取數據

現在我想從該響應獲取一個特定的值,但在互聯網上搜索後,掙扎了很多無法修復它。

這裏是我的代碼:

的反應是這樣的:

{"#error":false,"#data":{"sessid":"c4likn6vg33hngbpmobisrsbpcjjmf39","user":{"uid":0,"hostname":"102.119.85.120","roles":{"1":"anonymous user"},"session":"","cache":0}},"#response_code":200} 

從我要取sessid值以上回應。 什麼是正確的方法?

回答

4

可以在jsfiddle.net上查看和測試以下內容。

// Given a string of JSON called responseText 
var responseText = '{"#error":false,"#data":{"sessid":"c4likn6vg33hngbpmobisrsbpcjjmf39","user":{"uid":0,"hostname":"102.119.85.120","roles":{"1":"anonymous user"},"session":"","cache":0}},"#response_code":200}'; 

// You can parse it to an object using JSON.parse 
var responseObj = JSON.parse(responseText); 

// And then access the properties as with any object 
console.log(responseObj ["#data"]["sessid"]); 
+0

問題是當我嘗試保存我的響應像'var myData = this.responseText'然後我登錄它時,它顯示爲空。我不知道它爲什麼顯示爲空,但是當我將它記錄爲'this.responseText'時,它給了我確切的迴應 – 2012-02-03 14:20:40

+0

感謝它的工作......非常感謝.... – 2012-02-03 14:24:55