javascript - jquery display formatted json -


Hello I have a problem, because my json is not formatted as json displayed.

In my web page, I have a gt; & Lt; / Pre & gt; tag, it combines the json string:

json example:

  {"status", "output": {"pool": [ {"Stats": {"byte_usece": 0, "objects": 0, "kb_use": 0}, "name": "data", "id": "0", {"statistics": {"byte_usege" : 0, "objects": 0, "kb_used": 0}, "name": "metadata", "id": 1}, {"stats": "", "objects": 0, "kb_use": 0 }, "Numeric": "id" Pre> 

I use jquery script to format it ूं:

  var jsonPretty = JSON.stringify ($ (this) .text (), tap, '\ t'); $ (This) .text (jsonPretty);  

But the result is not running:

  "{\" status \ ": \" OK \ ", \" output \ ": {\" pool \ ":" \ "Data \": 0, \ "objects \": 0, \ "kb_used \": 0}, \ "name \": \ "data \", \ " "Id \": 0}, {\ "stats \": {\ "bytes_used \": 0, \ "objects \": 0, \ "kb_used \": 0}, \ "name \": \ "metadata \ ", \" Id \ ": 1}, {\" stats \ ": {\" bytes_used \ ": 0, \" objects \ ": 0, \" kb_used \ ": 0}, \" name \ " : \ "Rbd \", \ "id \": 2}], \ "stats \": {\ "total_used \": 63330648, \ "total_space \": 125604864, \ "total_avail \": 62274216}}} " 

I have to format it formatted Jason I Ase can format?

JSON.stringify takes an object, but you pass it a string To use this approach, you have to convert your string into an object, then back in the string:

  var jsonStr = $ ("pre") . Text (); Var jsonObj = JSON.parse (jsonStr); Var jsonPretty = JSON.stringify (jsonObj, null, '\ t'); . $ ("Pre") text (jsonPretty);  

Comments