JSON.stringify output to div in pretty print way

后端 未结 12 2408
野趣味
野趣味 2020-11-29 18:17

I JSON.stringify a json object by

result = JSON.stringify(message, my_json, 2)

The 2 in the argument above is su

12条回答
  •  星月不相逢
    2020-11-29 18:46

    A lot of people create very strange responses to these questions that make alot more work than necessary.

    The easiest way to do this consists of the following

    1. Parse JSON String using JSON.parse(value)
    2. Stringify Parsed string into a nice format - JSON.stringify(input,undefined,2)
    3. Set output to the value of step 2.

    In actual code, an example will be (combining all steps together):

        var input = document.getElementById("input").value;
        document.getElementById("output").value = JSON.stringify(JSON.parse(input),undefined,2);
    

    output.value is going to be the area where you will want to display a beautified JSON.

提交回复
热议问题