JavaScript格式化輸出並展示Json物件
阿新 • • 發佈:2018-12-16
效果就是下面這樣子,開始還以為程式碼很複雜,實際上一句JSON.stringify(data, null, "\t")
就解決了。
具體程式碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script> $(function () { var data = { name: "Apple", age: "18", location: "Beijing" }; $("#search_one_result").val(JSON.stringify(data, null, "\t")); $("#search_one_result").val(JSON.stringify(data, null, 4)); }); </script> </head> <body> <textarea id="search_one_result" cols="50" rows="10" style="margin-top: 30px"></textarea> </body> </html>