首页 > 代码库 > JS对象与json字符串格式

JS对象与json字符串格式

 1 <head> 2     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 3     <title></title> 4     <script type="text/javascript"> 5         var obj = new Object(); 6         obj.Name = "八戒" 7         obj.Age = 500; 8  9         //通过字面量的形式来定义对象10         var obj1 = { "Name": "八戒", "Age": "500" };11         var arr = [{ "Name": "八戒", "Age": "500" }, { "Name": "八戒1", "Age": "1000" }];12 13         //JSON格式:将json的字面量表示形式,当做字符串存放,则为json格式字符串14         var str = { "Name": "八戒", "Age": "500" };15         var jsonstrarr = [{ "Name": "八戒", "Age": "500" }, { "Name": "八戒1", "Age": "1000" }];;16         17         //将json字符串转换成js对象(数组)18         var resobj = JSON.parse(str);19         alert(resobj.Name);20 21     </script>22 </head>23 <body>24 </body>25 </html>
jsobAndJson

 

JS对象与json字符串格式