if you wanted the "child" property of the "data" object, under NORMAL circumstances, you'd type:
Code:
data["child"]
//or
data.child
so, when you type (or, by inference, do what you're trying to do)
Code:
data["child[0]"]
you'd get "undefined" because the data object contains a property called "child", but it has no property called "child[0]"
Code:
data = {"child[0]": 1234}
NOW data has a property called "child[0]". NOW this will work:
Code:
var i = "child[0]"
data[i]
// but data.i won't work because there is no property 'i'
so to TRULY get "index zero" of the "child" property of the "data" object, you'd have to use this syntax:
Code:
data["child"][0]
and the only way to eval that would be if you had something like
Code:
var i = '["child"][0]'
eval("data"+i)
1. If you reply to my post, and your reply would then appear directly beneath my post, DON'T QUOTE MY ENTIRE POST!!! IT'S REDUNTANT!!! IT'S ASININE!!!! IT'S REDUNDANTLY ASININE!!!!! DON'T DO IT!!!!
2. jQuery extends the functionality of JavaScript. If you don't know JavaScript, give up on that jQuery script and learn JavaScript. You'll save yourself a lot of frustration, I promise.
3. Use the [code][/code] tags. Otherwise, you may be left wondering why no one responded to your eyesore of a thread.
Whoa, listen to what Angry Black Man (love the name by the way!!) is saying - he's not saying to use eval because you don't need to. He did offer that option as a way to do it, but the proper way to do it, which he also pointed out, is this:
Code:
data.child[0];
//or
data['child'][0];
I've switched careers...
I'm NO LONGER a scientist,
but now a web developer...
awesome.
I think you may be missing a good JSON foundation. A JSON string, when eval'd, is simply an object literal in JavaScript. An object literal has properties and these properties have values. That's it. If you come from another programming language then you may say, 'hey, that's an associative array!' or 'hey, that's just a hash!' and for all intents and purposes, you would be correct, that's it. A JSON string is just a multi-dimensional array.
The properties (or keys) will always be strings (if they were all integers then you'd just be using a single-dimensional array, right?). The values, however, can be many different things. The values can themselves be object literals (or hashes), or they could be arrays, or simple types like strings or integers.
JSON is a very powerful and lightweight notation, the only way to use it properly, though is to have a good foundation in what a JavaScript object literal and a JavaScript array actually are and how they work.
Good luck!
I've switched careers...
I'm NO LONGER a scientist,
but now a web developer...
awesome.
Bookmarks