1

I've seen similar posts to this but I just can't get this piece of code right.

var obj2 = JSON.parse('{"venue_data": 
           {"venue_id":"25",
           "description":"Space Cafe",
          "venue_type": [
           {"type_description":"Cafe"},
           {"type_description":"Free Wifi"},
           {"type_description":"Hangout"}
                       ]
                  }
                        }
           ');

//next line doesn't work :(                         
alert(obj2.venue_data[0].venue_id);
//either do the next two :(                         
alert(obj2.venue_data[0].venue_type[0]);
                         }
alert(obj2.venue_data[0].venue_type[1]);

I've tries different things but now I'm just guessing.

ps...without the array in the data it works fine.

Any help welcome.

Thanks,

Ned

1
  • Welcome to Stack Overflow. Do any errors show up in the console? Errors are a very useful tool to diagnose issues with your code. Commented Feb 12, 2015 at 10:19

4 Answers 4

5

venue_data is an object, not an array. You cannot reference it using the [0] selector.

Instead, just use the literal:

alert(obj2.venue_data.venue_id);
Sign up to request clarification or add additional context in comments.

Comments

1

Yes because venue_data is not an array it is just an object.

Access it like console.log(obj2.venue_data.venue_id);

Instead if it would have been

var obj2 = JSON.parse('{"venue_data": ' + 
                            '[{"venue_id":"25",' +
                            '"description":"Space Cafe",' +
                            '"venue_type": [' +
                                            '{"type_description":"Cafe"},' +
                                            '{"type_description":"Free Wifi"},' +
                                            '{"type_description":"Hangout"}' +
                                          ']' +
                            '}]' +
                        '}');

Then you should have used [0] after venue_data

Comments

1

It seems that there are some type-errors in your code, and

alert(obj2.venue_data[0].venue_id);

will not work because

obj2.venue_data 

is not an array.

Here you go:

var obj2 = 
{
    "venue_data":
    {
        "venue_id": "25",
        "description": "Space Cafe",
        "venue_type": [
            { "type_description": "Cafe" },
            { "type_description": "Free Wifi" },
            { "type_description": "Hangout" }
        ]
    }
};

alert(obj2.venue_data.venue_id);                 
alert(obj2.venue_data.venue_type[0]);
alert(obj2.venue_data.venue_type[1]);



var obj2witharray = {
    "venue_data":[
     {
        "venue_id": "25",
        "description": "Space Cafe",
        "venue_type": [
            { "type_description": "Cafe" },
            { "type_description": "Free Wifi" },
            { "type_description": "Hangout" }
        ]
     },{
        "venue_id": "26",
        "description": "Universe Cafe",
        "venue_type": [
            { "type_description": "Cafe" },
            { "type_description": "Free Wifi" },
            { "type_description": "Hangout" }
        ]
     }, {
         "venue_id": "27",
         "description": "Earth Cafe",
         "venue_type": [
             { "type_description": "Cafe" },
             { "type_description": "Free Wifi" },
             { "type_description": "Hangout" }
         ]
     }
   ]
}

2 Comments

Thanks alot, yes the square brackets around the venue makes sense and has used it now.
obj2 doesn't need JSON.parse, it's not a string in your code.
0

Thanks Guys alot for both answers. My script had two issues: 1. The venue data was not properly defined and I should have had [ brackets so show the array of venues. 2. I was not creating the json object out out a single string and should have concatenated the strings.

Once again thanks alot. Ned

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.