Udders
Udders

Reputation: 6976

JSON can't access data returns undefined

I have the following code,

var json = [
    {
        "name": "Fashion Forward",
        "good": {
            "doors" : {
                "name1" : "ff_good_doors_1.jpg",
                "name2" : "ff_good_doors_2.jpg",
                "name3" : "ff_good_doors_3.jpg"
            }
        },
        "better": {

        },
        "best": {

        }

    }
]

I would expect to be able get data out by doing something like,

json.name which I would expect to contain "Fashion Forward" - however I get undefined return, but if I console.log(json) I can see that it is an object.

Where am I going wrong?

Upvotes: 0

Views: 104

Answers (4)

Pejman
Pejman

Reputation: 2636

Why you use Array ? if you want to access members like what you already said :

I would expect to be able get data out by doing something like,

json.name which I would expect to contain "Fashion Forward" - however I get undefined return, but if I console.log(json) I can see that it is an object.

Where am I going wrong?

use this code and remove array:

var json =    {
   "name": "Fashion Forward",
   "good": {
       "doors" : {
           "name1" : "ff_good_doors_1.jpg",
           "name2" : "ff_good_doors_2.jpg",
           "name3" : "ff_good_doors_3.jpg"
       }
   },
   "better": {},
   "best": {}
}

Now you may use json.name

Upvotes: 1

Kanisq
Kanisq

Reputation: 232

json is the name of the array, you can use like this json[0].name;

Upvotes: 3

sjkm
sjkm

Reputation: 3937

the json variable is an array. to get the first object you need to select it like this json[0] and then you can access the the name property like this

var name = json[0].name;

Upvotes: 0

Sachin Jain
Sachin Jain

Reputation: 21842

As it looks from your code, json is an array of single item. Try this

json[0].name

Let me know if it works for you.

Upvotes: 0

Related Questions