编程语言
首页 > 编程语言> > javascript-从JSON.parse数组中获取数据

javascript-从JSON.parse数组中获取数据

作者:互联网

我陷入了这个问题,我正在调用一个向我返回json响应的webService.

现在,我想从该响应中获取特定的价值,但是在互联网上搜索并苦苦挣扎之后,无法解决它.

这是我的代码:

var xhr = Titanium.Network.createHTTPClient({

onload : function(e) {
     Ti.API.info("Received text: " + this.responseText);
     alert('success');
     },
 // function called when an error occurs, including a timeout
 one rror : function(e) {
     Ti.API.debug(e.error);
     alert('error');
 },
 timeout : 5000
});

var data = {"data":"system.connect"};
xhr.open("POST","http://mytesturl.net/services/json");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
xhr.send("method=system.connect");

响应是这样的:

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

从以上响应中,我想获取sessid值.
什么是正确的方法?

解决方法:

可以在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"]);

标签:javascript,json,appcelerator,titanium
来源: https://codeday.me/bug/20191013/1907568.html