Search code examples
jqueryjsontraversalgetelementsbyname

jQuery and JSON: getting an element by name


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?


Solution

  • 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.