Web page JSON formatter

Source: Internet
Author: User

Plugin source code
Define a JavaScript class Var jsonfomartter ()  = {//Main method, parameter 1 is the JSON object you need to convert, note that the object is not a string, the second argument is the indent representation string you want, For example, you can pass in the "    " four spaces as indentation standard format:function (JSONOBJECT, INDEXSTR)  {     //If standard parameters are not indented, the default processing     if  (ARGUMENTS.LENGTH < 2)  {         var indexStr =  "";    }     var indexStrStyle =  "    ";     // Gets the type of the JSON object, such as array     var objecttype = gettype (Jsonobject), by calling method GetType;     //  handling by JSON object type     if  (objecttype ==  "Array ")  {        if  (jsonobject.length == 0)  {             return  "[]";         }        var shtml =  "[";     } else  {        var iCount = 0;         $.each (Jsonobject, function ()  {             iCount++;             return;        });         if  (icount == 0)  { // object is empty             return  "{}";         }        var sHTML =  "{";     }    //  traverse the key and value in the object and add indents     var iCount = 0;     $.each (JsonoBject, function (Skey, vvalue)  {        if  (iCount  > 0)  {            shtml +=   ",";         }        if   (objecttype ==  "array")  {             sHTML +=  ("\ n"  + indexstr + indexstrstyle);         } else {             sHTML +=  ("\ n"  + indexStr + indexStrStyle +  "\" " +  skey +  "\" " + ":  ");        }         // display relevant data type         switch  (GetType (vvalue))  {            case   "Array":            case  "Object":                 shtml +=  formatjson (vvalue,  (indexstr + indexstrstyle));                 break;             case  "Boolean":             case  "Number":                 shtml += vvalue.tostring ();                 break;             case  "NULL":                shtml +=   "NULL";                 break;            case  "string":                 sHTML +=  ("\" "  + vValue +  "\" ");                 break;            default:                 shtml +=   ("typeof: "  + typeof (vvalue));        }         icount++;    });    if  (objecttype ==  " Array ")  {        sHTML +=  (" \ n " + indexstr  +  "]");    } else {         shtml +=  ("\ n"  + indexStr +  "}");    }     return shtml;},//gets the object type gettype:function (obj)  {  if  (typeof (obj)  ==  " Object ")  {    if  (obj === null)  return " null ";     if  (obj.constructor ==  (New array). Constructor)  return  "Array";     if  (obj.constructor ==  (New date) constructor)  return  " Date ";    if  (obj.constructor ==  (New regexp). Constructor)  return   "Regex";    return  "Object";   }  return typeof (obj);}     } 

How to use

Call Jsonformatter.format directly (object, "");

For example there is such a JSON string var json = "{' name ': ' Harry ', ' sex ': ' Body '}";//Call Jsonformatter.format (Json.parse (JSON));

Optimization

With the highlight.js effect is better, as follows:

{"Result": [{"Name": "Spit Slot", "id": "336EFBA7-259D-4057-AE1E-679E81419CD7" , "module": "Life"}]}


Web page JSON formatter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.