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,&NBSP;INDEXSTR) { //If standard parameters are not indented, the default processing if (ARGUMENTS.LENGTH&NBSP;<&NBSP;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