I made a small script that iterates and prints the values of one JSON object, if the value of the object is another object or array, 'll call this work to iterate over the value (array or object).
This I pose a problem when I print the values of an array and print the values that also add values to __ proto__ as part of the array Note that to get this code you must have an element <ul>in your HTML with id jsonUls. The code:
var json = '{"jsonObject":{"value1":"value2", "array1" : ["value1","value2","value3"]}}';
var element = $('#jsonUls');
var object = JSON.parse(json);
recursivePrintObjectValues(object, element);
function recursivePrintObjectValues(object, element) {
for(key in object) {
var value = object[key];
if(typeof value === 'object' || Array.isArray(value)){
var random = new Date().valueOf() * Math.random() + 351;
appendLi(key, '', element);
var ul = appendUl(random, element);
recursivePrintObjectValues(value, ul);
} else {
appendLi(key, value, element);
}
}
}
function appendUl(random, element) {
var ul = $('<ul></ul>', {
id: random
})
ul.appendTo(element);
return ul;
}
function appendLi(key, value, element) {
if(value == '[object Object]') {
value = '';
}
$('<li></li>', {
text: key + ' : '+ value
}).appendTo(element);
}
The result is the following, and I can’t understand why and how to avoid it, I hope someone can explain to me.
<pre>
<ul id="jsonUls">
<li>jsonObject : </li>
<ul id="448773395479.7797">
<li>value1 : value2</li>
<li>array1 : </li>
<ul id="295240780591.31195">
<li>0 : value1</li>
<li>1 : value2</li>
<li>2 : value3</li>
<li>$pnmap$ : function $$JSCompiler_prototypeAlias$$$$pnmap$$($f$$30$$,$opt_obj$$28$$){return $goog$array$map$$.apply($JSCompiler_alias_NULL$$,$pn$aargs_$$(this,arguments))}</li>
<li>$pnforEach$ : function $$JSCompiler_prototypeAlias$$$$pnforEach$$($f$$32$$,$opt_obj$$30$$){$goog$array$forEach$$.apply($JSCompiler_alias_NULL$$,$pn$aargs_$$(this,arguments));return this}</li>
<li>$pnequals$ : function $$JSCompiler_prototypeAlias$$$$pnequals$$($arr2$$13$$,$opt_equalsFn$$2$$){return $goog$array$equals$$.apply($JSCompiler_alias_NULL$$,$pn$aargs_$$(this,arguments))}</li>
<li>$pnfindIndex$ : function $$JSCompiler_prototypeAlias$$$$pnfindIndex$$($f$$41$$,$opt_obj$$43$$){return $goog$array$findIndex$$.apply($JSCompiler_alias_NULL$$,$pn$aargs_$$(this,arguments))}</li>
<li>$pnindexOf$ : function $$JSCompiler_prototypeAlias$$$$pnindexOf$$($obj$$76$$,$opt_fromIndex$$10$$){return $goog$array$indexOf$$.apply($JSCompiler_alias_NULL$$,$pn$aargs_$$(this,arguments))}</li>
</ul>
</ul>
</ul>
</pre>