I'm currently trying to make a JSON parser and i'm facing a bit of trouble. Let's say you have a JSON, the size is unknown and its values are unknown as well (well they aren't but this isn't relevant), plus you don't know anything about its structure, nor its depth.
I'd like to retrieve every key
and values
of this JSON, if the values
are an array or an object, retrieve it's key
and values
as well.
Is there a generic way of doing this? So far this is what I have:
function parseJson(contents) {
var parsedJson = $.parseJSON(contents);
for(var elem in parsedJson){
for(var subelem in parsedJson[elem]){
for(var subsubelement in parsedJson[element][subelem]){
/* magic happens */
}
}
}
}
But, I immediately realize that I'm going the wrong way because I have no way of telling how the JSON will be built, therefore I don't know how many loops I'll need.