admin管理员组文章数量:1352865
I have a bunch of json files that I need to read and save as a text file. The problem is the names of the keys in each json file differs. I've seen the use of the function Object.keys to obtain the key names, but, for example, in such a file:
{
"mainKey1" :
[
{
"subKey1" : "Value 1",
"subKey2" : "Value 2",
"subKey3" : "Value 3"
},
{
"subKey1" : "Value 1",
"subKey2" : "Value 2",
"subKey3" : "Value 3"
}
],
"mainKey2" :
[
{
"subKey1" : "Value 1",
"subKey2" : "Value 2",
"subKey3" : "Value 3"
},
{
"subKey1" : "Value 1",
"subKey2" : "Value 2",
"subKey3" : "Value 3"
}
]
}
How could I get the names "mainKey1","mainKey2", and so on, and also "subKey1", subKey2", and so on.
Finally, after obtaining these key names, how could I use them to read the corresponding "Value1", "Value2", "Value3".
Thanks in advance!
I have a bunch of json files that I need to read and save as a text file. The problem is the names of the keys in each json file differs. I've seen the use of the function Object.keys to obtain the key names, but, for example, in such a file:
{
"mainKey1" :
[
{
"subKey1" : "Value 1",
"subKey2" : "Value 2",
"subKey3" : "Value 3"
},
{
"subKey1" : "Value 1",
"subKey2" : "Value 2",
"subKey3" : "Value 3"
}
],
"mainKey2" :
[
{
"subKey1" : "Value 1",
"subKey2" : "Value 2",
"subKey3" : "Value 3"
},
{
"subKey1" : "Value 1",
"subKey2" : "Value 2",
"subKey3" : "Value 3"
}
]
}
How could I get the names "mainKey1","mainKey2", and so on, and also "subKey1", subKey2", and so on.
Finally, after obtaining these key names, how could I use them to read the corresponding "Value1", "Value2", "Value3".
Thanks in advance!
Share Improve this question asked May 21, 2017 at 11:48 iSofiaiSofia 1,5322 gold badges23 silver badges40 bronze badges 2- Why don't use JSON.parse w3schools./js/js_json_parse.asp? – Jayakrishnan Commented May 21, 2017 at 11:53
- Does your question include reading and writing json files? Or just the bit about getting the object keys/values? – Logic Artist Commented May 21, 2017 at 11:54
2 Answers
Reset to default 7You can use Object.keys(obj) to get the keys:
var obj = {"mainKey1" :
[
{
"subKey1" : "Value 1",
"subKey2" : "Value 2",
"subKey3" : "Value 3"
},
{
"subKey1" : "Value 1",
"subKey2" : "Value 2",
"subKey3" : "Value 3"
}
]
}
var keys = Object.keys(obj);
console.log(keys[0]);
var subkeys = Object.keys(obj[keys[0]][0]);
console.log(subkeys);
console.log(subkeys[0]);
console.log(obj[keys[0]][0][subkeys[0]]);
Just toss that into a foreach loop to go through each available key/subkey and you can get the information you're looking for.
var obj = {
"mainKey1": [{
"subKey1": "Value 1",
"subKey2": "Value 2",
"subKey3": "Value 3"
},
{
"subKey1": "Value 1",
"subKey2": "Value 2",
"subKey3": "Value 3"
}
],
"mainKey2": [{
"subKey1": "Value 1",
"subKey2": "Value 2",
"subKey3": "Value 3"
},
{
"subKey1": "Value 1",
"subKey2": "Value 2",
"subKey3": "Value 3"
}
]
};
This will print all the values from above object.
for (var key in obj) {
var innerArray = obj[key];
for (var arrayKey in innerArray) {
var innerObj = innerArray[arrayKey]
for (var innerKey in innerObj) {
console.log(innerKey,innerObj[innerKey]);
}
}
}
running example - https://jsfiddle/voxf7do6/1/
本文标签: javascriptGetting amp Using JSON Object Key NamesStack Overflow
版权声明:本文标题:javascript - Getting & Using JSON Object Key Names - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743875983a2554331.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论