I have the following JSON:
var json = { "system" : { "world" : { "actions" : { "hello" : { "src" : "hello world/hello world.js", "command" : "helloWorld" } } } } }
I have the following javascript:
var x = "system";
// get the contents of system by doing something like json.getElementByName(x)
How do I get the contents of system using json
and x
in jQuery?
Just use:
var x = "system";
json[x];
It is a key/value
system of retrieval, and doesn't need a function call to use it.
Well to my knowledge jQuery doesn't navigate arbitrary objects like that - just the DOM. You could write a little function to do it:
function findSomething(object, name) {
if (name in object) return object[name];
for (key in object) {
if ((typeof (object[key])) == 'object') {
var t = findSomething(object[key], name);
if (t) return t;
}
}
return null;
}
It should be obvious that I haven't put that function through an elaborate QA process.
Try using JSON Path, it is like XPath expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With