从JSON.parse数组中获取数据 [英] Fetching data from JSON.parse array
本文介绍了从JSON.parse数组中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了这个问题,我正在调用一个返回json响应的webService。
I am stuck in this problem, I am calling a webService that returns me a json response.
现在我想从该响应中获取特定值但是之后在互联网上搜索并挣扎很多都无法修复它。
now i want to fetch a particular value from that response but after searching on internet and struggling a lot couldn't fix it.
这是我的代码:
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
onerror : 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
值。
什么是正确的方法?
from the above response I want to fetch the sessid
value.
what is the right approach?
推荐答案
以下内容可以在 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"]);
这篇关于从JSON.parse数组中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文