JSON to HTML - Display JSON as a list in HTML

Many-a-times it is required to print list out your json object in html, like when you are testing your API and api produces the out put as json. The following javascript function may be used to print your json object as a neat html un-ordered list

function json_to_html(json) 
{

   ret = "";
   ret += "<ul>";
   for( i in json) 
   {
      ret += "<li>"+i+": ";

      if( typeof json[i] === "object") 
         ret += json_to_html(json[i]);
      else 
         ret += json[i];
          
     ret += "</li>";
   }
   ret += "</ul>";
   return ret;
}

No comments:

Post a Comment