Thursday 15 March 2012

HTML5 JSON MultiDimensional Arrays Keys with javascript or jQuery? -



HTML5 JSON MultiDimensional Arrays Keys with javascript or jQuery? -

i read lots of info on net , forums cannot solution problem. have next json file:

var info = { "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 } ] } ] } ] }

i have validated json file. want keys first "aa" "a1" , "a2" , "ab1", "ab2" , etc. there questions , info values not keys. jquery might help confused how keys.

any ideas? hope...!

the key getting keys object of unknown size recursion. here have 2 solutions; 1 functional , 1 object oriented. created other info test in add-on yours.

here repo finish code: https://github.com/vasilionjea/json-keys

the data: var info = { 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 } ] } ] } } }

the functional way: /* * functional example. * retrieves keys object of unknown size using recursion. */ function _isobject(obj) { homecoming 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 array , each array's item object, // `collectkeys` function receiving each object of array argument. data[key].foreach(collectkeys); } }); homecoming _container; } // execute function var mykeys = collectkeys(data);

the object oriented way: /* * object oriented example. * retrieves keys object of unknown size using recursion. */ function jsonkeys(obj) { this._container = []; if (this._isobject(obj)) { // normal object literal. this._data = obj; } else if (typeof obj === 'string') { // in case info passed in valid json string. this._data = json.parse(obj); } else { throw new error('the provided argument must object literal or json string.'); } } jsonkeys.prototype = { constructor: jsonkeys, _isobject: function(obj) { homecoming object.prototype.tostring.call(obj) === "[object object]"; }, _eachkey: function(obj, callback) { // using `bind(this)` makes sure `this` value in `_collect` method // isn't set global object (window). object.keys(obj).foreach(callback.bind(this)); }, _recur: function(key, data) { // if key's value object go deeper. if (this._isobject(data[key])) { this._collect(data[key]); } if (array.isarray(data[key])) { // * because we're looping through array , each array's item object, // `_collect` method receiving each object of array argument. // * using `bind(this)` makes sure `this` value in `_collect` method // isn't set global object (window). data[key].foreach(this._collect.bind(this)); } }, _collect: function(data) { this._eachkey(data, function(key) { this._container.push(key); // go on collecting this._recur(key, data); }); }, getall: function() { this._collect(this._data); homecoming this._container; } }; // create new instance passing data. var keysobject = new jsonkeys(data); allkeys = keysobject.getall();

javascript jquery json

No comments:

Post a Comment