简体   繁体   中英

Parse the JSON Object with Index or position instead of keyname using Jquery?

My Existing Code and Explanation

Working Fine, But I want to Create a template like Its should read JSON Data that Having keys in any name , I am Always reading data from JSON object in 2nd Position

Now I read the JSON that passed in the argument (data) inside that using the for loop extract single object in group, after that get the data using key(Module_Name).. I know the key(Module_Name) is always present in second position only, Now its Working Fine with keyname rather i want to fetch the data using position, that also used as template in my other modules

/* render Screen Objects */
screenRenderer.displayScreens = function(data) 
{
     screenRenderer.renderLayout(function(cont, boo)
    {
        if (boo) 
        {

            for(i=0;i<data.length;i++)
            {

            var buttons = "<button class='screen_button'>"+data[i].Module_Name+"</button>";
                $("#screen-cont").append(buttons);

            }
        }
        else 
        {
            scr_cont.show();
        }
    })
}

This is My Requirement, kindly awaiting suggestions, answers.. Thanks in Advance

You can use the function Object.keys(obj) to get all keys of an object as array, which you can access with an Index and than use the String you get to access the property.

So it could look like this:

var obj = data[i];
var keys = Object.keys(obj);
var result = obj[keys[1]];

Hope this is what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM