I've searched for the answer to this question, in many places. However, the only thing I can find is how to get the values of all of a certain key in a JSON file. I would like to know if there is a way to get the value of one specific key. I have a JSON file set up like so:
{
"Category": [{
"Subcategory": {
"Item1": {
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
"key5": "value5",
"key6": "value6"
},
"Item2": {
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
"key5": "value5",
"key6": "value6"
}
}
}]
}
I would like to know a method using JQuery/Javascript to get, for example, the value of Key1 in Item1(value1).
您可以简单地执行以下操作:
Category[0].Subcategory.Item1.key1
Like other comments and answers, you can easily get the value for a specific hard-coded key like this:
Category[0].Subcategory.Item1.key1
But if you have dynamic keys, you can do like this:
var categories = {
"Category": [{
"Subcategory": {
"Item1": {
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
"key5": "value5",
"key6": "value6"
},
"Item2": {
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4",
"key5": "value5",
"key6": "value6"
}
}
}]
};
var itemKeyName = "Item1";
var keyName = "key1";
console.log(categories.category[0].Subcategory[itemKeyName][keyName];
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.