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:
我有一堆json文件,我需要阅读并保存为文本文件。问题是每个json文件中的键名称不同。我已经看到使用函数Object.keys来获取键名,但是,例如,在这样的文件中:
{
"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.
我怎么能得到名称“mainKey1”,“mainKey2”等,还有“subKey1”,subKey2“等等。
Finally, after obtaining these key names, how could I use them to read the corresponding "Value1", "Value2", "Value3".
最后,在获得这些键名后,如何使用它们来读取相应的“Value1”,“Value2”,“Value3”。
Thanks in advance!
提前致谢!
2 个解决方案
#1
1
You can use Object.keys(obj) to get the keys:
您可以使用Object.keys(obj)来获取密钥:
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.
只需将其放入foreach循环中即可浏览每个可用的键/子键,您就可以获得所需的信息。
#2
1
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.net/voxf7do6/1/
运行示例 - https://jsfiddle.net/voxf7do6/1/
#1
1
You can use Object.keys(obj) to get the keys:
您可以使用Object.keys(obj)来获取密钥:
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.
只需将其放入foreach循环中即可浏览每个可用的键/子键,您就可以获得所需的信息。
#2
1
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.net/voxf7do6/1/
运行示例 - https://jsfiddle.net/voxf7do6/1/