Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

javascript - 如何以编程方式美化JSON? [重复](How can I beautify JSON programmatically? [duplicate])

This question already has an answer here:

(这个问题在这里已有答案:)

Do you know of any "JSON Beautifier" for JavaScript?

(你知道JavaScript的“JSON Beautifier”吗?)

From

()

{"name":"Steve","surname":"Jobs","company":"Apple"}

To

()

{
  "name" : "Steve",
  "surname" : "Jobs",
  "company" : "Apple"
}

Example

()

some_magic(jsonObj); // return beautified JSON
  ask by Randy Mayer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Programmatic formatting solution:(编程格式化解决方案:)

The JSON.stringify method supported by many modern browsers (including IE8) can output a beautified JSON string:

(许多现代浏览器(包括IE8)支持的JSON.stringify方法可以输出一个美化的JSON字符串:)

JSON.stringify(jsObj, null, ""); // stringify with tabs inserted at each level
JSON.stringify(jsObj, null, 4);    // stringify with 4 spaces at each level
(Demo: )http://jsfiddle.net/AndyE/HZPVL/

This method is also included with json2.js , for supporting older browsers.

(此方法也包含在json2.js中 ,用于支持旧版浏览器。)

Manual formatting solution(手动格式化方案)

If you don't need to do it programmatically, Try JSON Lint .

(如果您不需要以编程方式执行此操作,请尝试JSON Lint 。)

Not only will it prettify your JSON, it will validate it at the same time.

(它不仅会美化你的JSON,它还会同时验证它。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...