简体   繁体   中英

How to get dynamically changed Key's value in JSON String using jQuery

I'm trying to parse my JSON code through jQuery but I stuck with this situation.

My JSON code :

var options = 
    "Group": { /* This will dynamically change depending on user input */
        "Setting 1" : { /* This will dynamically change too depending on user input */
            "id": "related_posts",
            "name": "Related Posts",
            "desc": "Show Related Posts",
            "type": "checkbox",
            "std": 0
        },
        "Setting 2": { /* Also this will dynamically change depending on user input */
            "id": "breaking_news",
            "name": "Breaking News",
            "desc": "Hide Breaking News",
            "type": "checkbox",
            "std": 1
        }
    }

I tried this but it work only if it's constant :

options.Group["Setting 1"].id

In conclusion I want a method to get dynamic keys.

I forget to mention that I'm still learning jQuery so I don't have a lot of knowledge about it.

I was able to find a solution in StackOverflow after the direction of Pointy, so it looks like I had a lack of vocabulary to search very well.

for (var property in options) {
    if (options.hasOwnProperty(property)) {
        alert(property);
    }
}

Iterate through object properties

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