<! DOCTYPE HTML > < HTML > < head > < Meta charset = "UTF-. 8" > < title > novice tutorial (runoob.com) </ title > </ head > < body > < H2 > create JSON string Object </ H2 > < P ID = "Demo" > </ P > < Script > var text = ' { "sites":[' + '{ "name":"Runoob" , "url":"www.runoob.com" },' + '{ "name":"Google" , "url":"www.google.com" },' + '{ "name":"Taobao" , "url":"www.taobao.com" } ]}'; obj = JSON.parse(text); document.getElementById("demo").innerHTML = obj.sites[1].name + " " + obj.sites[1].url; </script> </body> </html>
<! DOCTYPE HTML > < HTML > < head > < Meta charset = "UTF-. 8" > < title > novice tutorial (runoob.com) </ title > </ head > < body > < H2 > with optional parameters, callback </ H2 > < P ID = "Demo" > </ P > < Script > the JSON.parse ( ' { "P":}. 5 ' ,function(k, v) { IF (K === '' ) { return V;} return V * 2 ; }); the JSON.parse ( ' { ". 1":. 1, "2": 2, ". 3": { ". 4":. 4 , ". 5": { ". 6":}}}. 6 ' , function (K, V) { document.write (K); // output current property, one of the last "" document.write ( " <br> " ); return V; // return the modified value }); </ Script > </ body > </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <p id="demo"></p> <script> var str = {"name":"菜鸟教程", "site":"http://www.runoob.com " } str_pretty1 = the JSON.stringify (STR) document.write ( " only one parameter case: " ); document.write ( " <br> " ); document.write ( " <pre > " + str_pretty1 + " </ pre> " ); document.write ( " <br> " ); str_pretty2 = the JSON.stringify (STR, null , . 4 ) // use 4 space indentation document.write ( "Use parameters are: " ); document.write( "<br>" ); document.write("<pre>" + str_pretty2 + "</pre>" ); // pre 用于格式化输出 </script> </body> </html>