2012-02-03 79 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