简体   繁体   English

如何从 JSON 响应中获取数据?

[英]How to get data from JSON response?

I am using plain JavaScript on my project.我在我的项目中使用纯 JavaScript。 How can I get the value of the following example with the category?如何使用类别获取以下示例的值? I need to detect whether it comes back true or false.我需要检测它是真的还是假的。

{
    "category": "true"
}

I can get the entire object, but I just want to pull out the value of category.我可以得到整个对象,但我只想拉出category的值。


from comment...从评论...

The JSON data is returned from the server based on a form submission. JSON 数据是基于表单提交从服务器返回的。 It keeps saying myObject is undefined.它一直说 myObject 未定义。 How can do I pass this so my JavaScript can read the response?我该如何传递它以便我的 JavaScript 可以读取响应?

from comment...从评论...

I can get myObject using this: if (form.XHR.status === 200) {var data = form.XHR.response;} , but if I try to do data.myObject it says it's undefined.我可以使用这个获取 myObject: if (form.XHR.status === 200) {var data = form.XHR.response;} ,但如果我尝试做data.myObject它说它是未定义的。

You need to parse the JSON before you can access it as an object...您需要先解析 JSON,然后才能将其作为对象访问...

if (form.XHR.status === 200) {
    var data = form.XHR.response;

    var parsed = JSON.parse(data);

    alert(parsed.category);
}

Why is this needed?为什么需要这个? It's because JSON is not JavaScript .这是因为JSON 不是 JavaScript The two terms are not synonymous.这两个术语不是同义词。

JSON is a textual data interchange format. JSON 是一种文本数据交换格式。 It needs to be parsed into the data structures of whatever language it's been given to.它需要被解析任何语言的数据结构。 In your case, the language is JavaScript, so you need to parse it into JavaScript data.在您的情况下,语言是 JavaScript,因此您需要将其解析为 JavaScript 数据。

When it is received form the xhr response, it is received in the form in which all textual data is handled in JavaScript.当从 xhr 响应接收到它时,它以所有文本数据在 JavaScript 中处理的形式接收。 That is as a string .那是一个string As a string, you can't directly access the values represented.作为字符串,您不能直接访问所表示的值。

JavaScript has a built in parser called JSON.parse . JavaScript 有一个名为JSON.parse的内置解析器。 This was used in the example above to do the necessary conversion.这在上面的示例中用于进行必要的转换。

Some older browsers don't support JSON.parse .一些较旧的浏览器不支持JSON.parse If you're supporting those browsers, you can find a JavaScript parser at http://json.org .如果您支持这些浏览器,则可以在http://json.org 上找到 JavaScript 解析器。

First of all you need a variable to refer it:首先,您需要一个变量来引用它:

var obj = {
    "category": "true"
};

Then can you say eg:然后你可以说例如:

alert(obj.category);
var myObject = { "category": "true"};

alert (myObject.category);

But you likely want:但你可能想要:

var myObject = { "category": true};

...if you're going to be testing for true/false: ...如果你要测试真/假:

if (myObject.category) {
    // category is true, so do your stuff here.
}

You can access json object data using '.'您可以使用 '.' 访问 json 对象数据。 or [key] like this :或 [key] 像这样:

var obj = {
    "category": "true"
};
console.log(obj.category);   
// Or
console.log(obj["category"]);

Here is the DEMO这是演示

For anyone who arrives here banging their head against the wall, make sure to see if you need to access a parent object which wraps all the delivered data:对于到达这里的任何人,头撞墙,请确保查看您是否需要访问包装所有传递数据的父对象:

console.log(response['id'])

may not work, because a parent entity must be accessed first:可能不起作用,因为必须首先访问父实体:

console.log(response.session['id'])

If you console log your response and it is wrapped in {} you probably need to do this.如果您控制台记录您的响应并且它包含在{}您可能需要执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM