从 JSON.parse 数组中获取数据 [英] Fetching data from JSON.parse array

查看:65
本文介绍了从 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆