[英]How to access nested JSON object key and values
I am trying to access the key and values of my nested array like this: 我试图像这样访问我的嵌套数组的键和值:
var obj = $.getJSON("mydata.json", function() {
console.log( "load success" );
});
Object.keys(obj[2].type).forEach(function(key) {
console.log(key, obj[key]);
});
But I get an error. 但是我得到一个错误。
Here's the structure of the JSON file: 这是JSON文件的结构:
{
"nodes": [
{
"nd": "nd1",
"cat": "cat1"
}
],
"links": [
{
"id": 100
}
],
"types": [
{
"type": "one",
"image": "image001"
},
{
"type": "two",
"image": "image002"
},
{
"type": "three",
"image": "image003"
}
]
} }
My goal is to get a list of values of: 我的目标是获取以下值的列表:
one two three 一二三
Something like that? 这样的东西?
You have to firstly acces to object.types attribute, then iterate on it to retrieve every type. 您必须首先访问object.types属性,然后对其进行迭代以检索每种类型。
var object = { "nodes": [{ "nd": "nd1", "cat": "cat1" }], "links": [{ "id": 100 }], "types": [{ "type": "one", "image": "image001" }, { "type": "two", "image": "image002" }, { "type": "three", "image": "image003" }] }; function parseJson(object){ object.types.forEach(function(key) { console.log(key.type); }); } parseJson(object);
--- update to anwer question in comment --- -更新评论中的问题-
you could enclose the code in a function, and call it when you load your json: 您可以将代码封装在函数中,并在加载json时调用它:
$.getJSON("mydata.json", function(data) {
parseJson(data);
});
Object.keys(obj[2].type)
doesn't really make sense here. Object.keys(obj[2].type)
在这里没有任何意义。 You're not iterating over the properties of any object, you're accessing the same property in a set of objects. 您无需遍历任何对象的属性,而是要访问一组对象中的相同属性。 This is what map
does. 这就是map
作用。
var types = obj.types.map(x => x.type);
You can do it with this code: 您可以使用以下代码进行操作:
var obj = { "nodes": [ { "nd": "nd1", "cat": "cat1" } ], "links": [ { "id": 100 } ], "types": [ { "type": "one", "image": "image001" }, { "type": "two", "image": "image002" }, { "type": "three", "image": "image003" } ] }; Object.keys(obj["types"]).forEach(function(key) { console.log(obj["types"][key].image); });
$.getJSON returns a promise because it is asynchronous, but you can set your variable in your callback: $ .getJSON返回一个promise,因为它是异步的,但是您可以在回调中设置变量:
var obj;
$.getJSON("mydata.json", function(data) {
obj = data;
Object.keys(obj[2].type).forEach(function(key) {
console.log(key, obj[key]);
});
});
You should post the error that you get, but I can tell you that the problem is that your output code executes before the JSON object has been loaded. 您应该发布收到的错误,但是我可以告诉您问题是您的输出代码在加载JSON对象之前就已执行。 Try this: 尝试这个:
$.getJSON("mydata.json", function(obj) {
console.log( "load success" );
Object.keys(obj[2].type).forEach(function(key) {
console.log(key, obj[key]);
});
});
Here is a functional way to do it in js : 这是在js中实现此功能的方法:
var types = $.getJSON("mydata.json").done(function(response) {
return Object.keys(data["types"]).map(function(element){
return element.type;
}).join(' ');
});
console.log(types);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.