从 JSON.parse 数组中获取数据

发布于 2025-01-01 23:37:26 字数 959 浏览 0 评论 0原文

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

现在我想从该响应中获取特定的值,但在互联网上搜索并努力挣扎后无法修复它。

这是我的代码:

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 值。 什么是正确的做法?

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.

here is my code:

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");

The response is like this:

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

from the above response I want to fetch the sessid value.
what is the right approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假情假意假温柔 2025-01-08 23:37:26

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

The following can be viewed and tested on 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"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文