I am working on a project where I have to use data from the dynamic JSON file. This is example of how JSON file could look.
我正在开发一个项目,我必须使用动态JSON文件中的数据。这是JSON文件的外观示例。
{
"selectors": {
"title": {
"css": ".title"
},
"url": {
"css": ".url"
}
}
}
Because the content of JSON file is dynamic, I can't access object like for example:
因为JSON文件的内容是动态的,所以我无法访问对象,例如:
var data = '{"selectors":{"title":{"css":".title"},"url":{"css":".url"}}}';
var json = JSON.parse(data);
var element = json.selectors.title.css;
However, there are two objects that will always have same name, and those are selectors and css.
但是,有两个对象始终具有相同的名称,那些是selectors和css。
Now how can I access the objects title, url, category, time if they are dynamic? AND then to access css object trough those?
现在,如果它们是动态的,我如何访问对象标题,网址,类别和时间?然后通过那些访问css对象?
I need vanila JavaScript, not jQuery.
我需要vanila JavaScript,而不是jQuery。
1 个解决方案
#1
1
Because you don't know if a specific attribute exists you need to loop through all of them:
因为您不知道是否存在特定属性,所以您需要遍历所有属性:
var json = JSON.parse(data);
for (key in json) {
}
but to make sure that you only loop through valid objects (and not something someone else might have tampered with) you should check for hasOwnProperty:
但是为了确保只循环通过有效对象(而不是其他人可能已经篡改过的东西),你应该检查hasOwnProperty:
var json = JSON.parse(data);
for (key in json) {
if (json.hasOwnProperty(key) {
}
}
Now you are sure to loop through the first level of attributes, which in your example is only the attribute selectors
. Now you can add another loop to check the 2nd level of attributes:
现在,您确定循环遍历第一级属性,在您的示例中,这些属性仅是属性选择器。现在您可以添加另一个循环来检查第二级属性:
var json = JSON.parse(data);
for (key in json) {
if (json.hasOwnProperty(key) {
for (second_level in key) {
if (key.hasOwnProperty(second_level) {
if (second_level == 'title') { /* do something with title */ }
if (second_level == 'url') { /* do something with url */ }
}
}
}
}
This can get a little messy, but because you expect a specific structure, you can just check for those attributes:
这可能会有点混乱,但因为您期望特定的结构,您可以检查这些属性:
if (json.hasOwnProperty('selectors') {
if (json.selectors.hasOwnProperty('title') { /* do something with title*/ }
if (json.selectors.hasOwnProperty('url') { /* do something with url */ }
}
We can access the attributes title and url directly because in your json these are objects ({}
) and not arrays ([]
), if they were arrays you would need to loop through them.
我们可以直接访问属性title和url,因为在你的json中这些是对象({})而不是数组([]),如果它们是数组,你需要循环它们。
Hope it helps
希望能帮助到你
#1
1
Because you don't know if a specific attribute exists you need to loop through all of them:
因为您不知道是否存在特定属性,所以您需要遍历所有属性:
var json = JSON.parse(data);
for (key in json) {
}
but to make sure that you only loop through valid objects (and not something someone else might have tampered with) you should check for hasOwnProperty:
但是为了确保只循环通过有效对象(而不是其他人可能已经篡改过的东西),你应该检查hasOwnProperty:
var json = JSON.parse(data);
for (key in json) {
if (json.hasOwnProperty(key) {
}
}
Now you are sure to loop through the first level of attributes, which in your example is only the attribute selectors
. Now you can add another loop to check the 2nd level of attributes:
现在,您确定循环遍历第一级属性,在您的示例中,这些属性仅是属性选择器。现在您可以添加另一个循环来检查第二级属性:
var json = JSON.parse(data);
for (key in json) {
if (json.hasOwnProperty(key) {
for (second_level in key) {
if (key.hasOwnProperty(second_level) {
if (second_level == 'title') { /* do something with title */ }
if (second_level == 'url') { /* do something with url */ }
}
}
}
}
This can get a little messy, but because you expect a specific structure, you can just check for those attributes:
这可能会有点混乱,但因为您期望特定的结构,您可以检查这些属性:
if (json.hasOwnProperty('selectors') {
if (json.selectors.hasOwnProperty('title') { /* do something with title*/ }
if (json.selectors.hasOwnProperty('url') { /* do something with url */ }
}
We can access the attributes title and url directly because in your json these are objects ({}
) and not arrays ([]
), if they were arrays you would need to loop through them.
我们可以直接访问属性title和url,因为在你的json中这些是对象({})而不是数组([]),如果它们是数组,你需要循环它们。
Hope it helps
希望能帮助到你