最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get data from JSON response? - Stack Overflow

programmeradmin0浏览0评论

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.

{
    "category": "true"
}

I can get the entire object, but I just want to pull out the value of 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?

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.

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.

{
    "category": "true"
}

I can get the entire object, but I just want to pull out the value of 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?

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.

Share Improve this question edited Jun 27, 2012 at 16:35 user1106925 asked Jun 27, 2012 at 16:20 ZoolanderZoolander 2,3635 gold badges27 silver badges37 bronze badges 4
  • Is that a string, or an object? – Jonathan M Commented Jun 27, 2012 at 16:23
  • 1 foo.category? Also, try harder .. that is really basic and accessible through any search engine. – yoda Commented Jun 27, 2012 at 16:24
  • 1 Has your JSON been parsed? If so, is it that you don't know how to access object properties? Or is it that you do know how, but it isn't giving you the expected output. Please provide some relevant information when you ask a question. – user1106925 Commented Jun 27, 2012 at 16:31
  • @am not i am: That was my problem. Thank you! – Zoolander Commented Jun 27, 2012 at 17:05
Add a comment  | 

5 Answers 5

Reset to default 5

You need to parse the JSON before you can access it as an object...

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 is a textual data interchange format. 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.

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.

JavaScript has a built in parser called JSON.parse. This was used in the example above to do the necessary conversion.

Some older browsers don't support JSON.parse. If you're supporting those browsers, you can find a JavaScript parser at http://json.org .

First of all you need a variable to refer it:

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

Then can you say e.g:

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 '.' or [key] like this :

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.

发布评论

评论列表(0)

  1. 暂无评论