show nested json data in treeview in javascript -
i have nested json data. used blow function.
var jsonsource={"error_code":0, "ext_info":{"name":{"firstname":"john","lastname":"jonson","nickname":"jj"}}}; var obj=json.parse(jsonsource),returnvalue; function showjson(obj){ for(var key in obj){ if(typeof obj[key]==='object'){ returnvalue+='<div>'+key+'/\n'; showjson(obj[key]); returnvalue+='</div>'; } else{ returnvalue+=key+'equal'+obj[key]; } } docoument.getelementbyid('data').innerhtml=returnvalue; }
as said before , have large nested json data , when parse showjson function ,it shows 1 level of json data , puts others deep level of datajson undefined. should resolve problem?
recursive approach works more intuitively when done actual return values. have @ https://jsfiddle.net/ughnjfh0/1/
var jsonsource='{"error_code":0, "ext_info":{"name":{"firstname":"john","lastname":"jonson","nickname":"jj"}}}'; var obj=json.parse(jsonsource); function showjson(obj){ var returnvalue=''; for(var key in obj){ if(typeof obj[key]==='object'){ returnvalue+='<div>'+key+'/\n'; returnvalue+=showjson(obj[key]); returnvalue+='</div>'; } else{ returnvalue+=key+'equal'+obj[key]; } } return returnvalue; } document.getelementbyid('data').innerhtml= showjson(obj);
also:
jsonsource
should string parsable json data- typo in
docoument.getelementbyid('data').innerhtml=returnvalue;
Comments
Post a Comment