HTML5 JSON多维数组键与javascript或jQuery
HTML5 JSON MultiDimensional Arrays Keys with javascript or jQuery?
我在网上和论坛上读了很多信息,但无法找到解决问题的方法。我有以下JSON文件:
var data =
{ "AA" :[{"a1":[{"ab1": [
{"ab1a": 10 },
{"ab1b": 20 },
{"ab1c": 30 },
{"ab1d": 40 }
]
},
{"ab2":[
{"ab1a": 10 },
{"ab1b": 20 },
{"ab1c": 30 },
{"ab1d": 40 }
]
}
]
}
,{"a2":[{"ab3": [
{"ab3a": 10 },
{"ab3b": 20 },
{"ab3c": 30 },
{"ab3d": 40 }
]
},
{"ab4":[
{"ab4a": 10 },
{"ab4b": 20 },
{"ab4c": 30 },
{"ab4d": 40 }
]
}
]
}
]
}
我已经验证了JSON文件。我想先获得"AA"
、"a1"
和"a2"
的密钥,然后再获得"ab1"
、"ab2"
等的密钥。有关于值的问题和信息,但没有密钥。jQuery可能会有所帮助,但我对如何获取所有密钥感到困惑
有什么想法吗?任何希望。。。!
从未知大小的对象中获取所有键的关键是递归。这里我有两个解决方案;一个是功能性的,一个是面向对象的。除了你的数据,我还创建了一些其他数据来测试。
以下是带有完整代码的回购:https://github.com/vasilionjea/json-keys
数据:
var data = {
continents: {
europe: {
countries: [
{
'country-name': 'italy',
cities: [
{ 'city-name': 'Rome', title: 'Roma Dolor', population:873, gdb: 301 },
{ 'city-name': 'Venice', title: 'Venice Veggies.', population:456, gdb: 244 }
]
},
{
'country-name': 'france',
cities: [
{ 'city-name': 'Paris', title: 'De Ipsum', population:7123, gdb: 77 },
{ 'city-name': 'Marseille', title: 'La Mipsum', population:73, gdb: 7 }
]
}
]
},
'north-america': {
countries: [
{
'country-name': 'canada',
cities: [
{ 'city-name': 'Montreal', title: 'The city of lorem ipsum!', population:99, gdb: 011 },
{ 'city-name': 'Quebec', title: 'Veggie Ipsum.', population:123, gdb: 101 }
]
},
{
'country-name': 'usa',
cities: [
{ 'city-name': 'New York', title: 'Empire State', population:1001001, gdb: 1010 },
{ 'city-name': 'San Francisco', title: 'SF Bridge', population:20123, gdb: 202 }
]
}
]
}
}
}
功能方式:
/*
* Functional example.
* Retrieves all keys from an object of unknown size using recursion.
*/
function _isObject(obj) {
return Object.prototype.toString.call(obj) === "[object Object]";
}
function _eachKey(obj, callback) {
Object.keys(obj).forEach(callback);
}
var _container = [];
function collectKeys(data) {
_eachKey(data, function(key) {
_container.push(key);
if (_isObject(data[key])) {
collectKeys(data[key]);
}
if (Array.isArray(data[key])) {
// Because we're looping through an array and each array's item is an object,
// the `collectKeys` function is receiving each object of the array as an argument.
data[key].forEach(collectKeys);
}
});
return _container;
}
// Execute the function
var myKeys = collectKeys(data);
面向对象的方式:
/*
* Object Oriented example.
* Retrieves all keys from an object of unknown size using recursion.
*/
function JSONKeys(obj) {
this._container = [];
if (this._isObject(obj)) {
// Just a normal object literal.
this._data = obj;
} else if (typeof obj === 'string') {
// Just in case the data was passed in as a valid JSON string.
this._data = JSON.parse(obj);
} else {
throw new Error('The provided argument must be an object literal or a JSON string.');
}
}
JSONKeys.prototype = {
constructor: JSONKeys,
_isObject: function(obj) {
return Object.prototype.toString.call(obj) === "[object Object]";
},
_eachKey: function(obj, callback) {
// Using `bind(this)` makes sure that the `this` value in the `_collect` method
// isn't set to the global object (window).
Object.keys(obj).forEach(callback.bind(this));
},
_recur: function(key, data) {
// If this key's value is also an object go deeper.
if (this._isObject(data[key])) {
this._collect(data[key]);
}
if (Array.isArray(data[key])) {
// * Because we're looping through an array and each array's item is an object,
// the `_collect` method is receiving each object of the array as an argument.
// * Using `bind(this)` makes sure that the `this` value in the `_collect` method
// isn't set to the global object (window).
data[key].forEach(this._collect.bind(this));
}
},
_collect: function(data) {
this._eachKey(data, function(key) {
this._container.push(key);
// Continue collecting
this._recur(key, data);
});
},
getAll: function() {
this._collect(this._data);
return this._container;
}
};
// Create a new instance passing the data.
var keysObject = new JSONKeys(data);
allKeys = keysObject.getAll();
相关文章:
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- Javascript(jQuery)给了我奇怪的结果
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- Javascript/jQuery中的并行Ajax调用
- 我可以在Javascript/jQuery中使用一个变量作为键吗
- Javascript/Jquery/PHP加载页面-如何
- Javascript/Jquery Blob not showing Chrome PDF
- 如何使用javascript/jquery获取iframe的URL的锚点属性
- OnClick/Onhover Javascript/jquery
- 如何从javascript/jquery中的复选框中获取布尔值
- javascript/jquery将utc转换为短格式的本地时间
- 在不设置协议的情况下,使用javascript/jquery更改iframe-src
- 如何向这个javascript/jquery函数添加参数
- 用JavaScript/jQuery点击图片,选中多个方框
- 使用Javascript/JQuery获取JSON GET数据
- 从数组中删除元素的最佳方法是:javascript/jquery
- Javascript/jQuery压缩一个图像正确的评论帖子,并使用Ajax Laravel 5.2保存到控制器中
- 用DRY方式Javascript/JQuery动态替换HTML
- 类似Javascript JQuery的库
- 如何在 Javascript/jQuery 中获取一些随机下拉列表的选定值