Javascript 如何从 JSON 响应中获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11230784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get data from JSON response?
提问by Zoolander
I am using plain JavaScript on my project. How can I get the value of the following example with the category? I need to detect whether it comes back true or false.
我在我的项目中使用纯 JavaScript。如何使用类别获取以下示例的值?我需要检测它是真的还是假的。
{
"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. It keeps saying myObject is undefined. How can do I pass this so my JavaScript can read the response?
JSON 数据是基于表单提交从服务器返回的。它一直说 myObject 未定义。我该如何传递它以便我的 JavaScript 可以读取响应?
from comment...
从评论...
I can get myObject using this:
if (form.XHR.status === 200) {var data = form.XHR.response;}
, but if I try to dodata.myObject
it says it's undefined.
我可以使用 this: 获取 myObject
if (form.XHR.status === 200) {var data = form.XHR.response;}
,但是如果我尝试这样做,data.myObject
它会说它是未定义的。
回答by
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. The two terms are not synonymous.
为什么需要这个?这是因为JSON 不是 JavaScript。这两个术语不是同义词。
JSON is a textual data interchange format. It needs to be parsed intothe 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.
JSON 是一种文本数据交换格式。它需要被解析成任何语言的数据结构。在您的情况下,语言是 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. That is as a string
. As a string, you can't directly access the values represented.
当从 xhr 响应接收到它时,它以所有文本数据在 JavaScript 中处理的形式接收。那是作为一个string
. 作为字符串,您不能直接访问所表示的值。
JavaScript has a built in parser called JSON.parse
. This was used in the example above to do the necessary conversion.
JavaScript 有一个内置的解析器,称为JSON.parse
. 这在上面的示例中用于进行必要的转换。
Some older browsers don't support JSON.parse
. If you're supporting those browsers, you can find a JavaScript parser at http://json.org.
一些较旧的浏览器不支持JSON.parse
. 如果您支持这些浏览器,则可以在http://json.org 上找到 JavaScript 解析器。
回答by Zoolander
First of all you need a variable to refer it:
首先,您需要一个变量来引用它:
var obj = {
"category": "true"
};
Then can you say e.g:
然后你可以说例如:
alert(obj.category);
回答by Jonathan M
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.
}
回答by Nishu Tayal
回答by SauerTrout
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'])
console.log(response['id'])
may not work, because a parent entity must be accessed first:
可能不起作用,因为必须首先访问父实体:
console.log(response.session['id'])
console.log(response.session['id'])
If you console log your response and it is wrapped in {}
you probably need to do this.
如果您控制台记录您的响应并且它包含在{}
您可能需要这样做。