<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> window.onload=function () { json={ "name":"杨过", "age":"23", "height":"120cm", "friends":[{ "name":"小龙女", "age":"32", "height":"150cm" },{ "name":"凤姐", "age":"32", "height":"140cm" }] } // var json1={} // for(x in json){ // json1[x]=json[x] // } // json1.name="尹志平" // json1.friends.name="尹志平" // document.write(JSON.stringify(json)+"<br>") // document.write(JSON.stringify(json1)) var json2=JSON.parse(JSON.stringify(json)); // stringify已经将json转换为字符串,parse又将字符串转换为了对象进行操作 json2.name="尹志平" json2.friends[0].name="尹志平" // 将json下一级的第一个数组改变 document.write(JSON.stringify(json)+"<br>") document.write(JSON.stringify(json2)) } </script> </head> <body> </body> </html>
json如何复制和复制下一级,且改变其中元素时,原json不会被改变
猜你喜欢
转载自blog.csdn.net/xinye666666/article/details/80696807
今日推荐
周排行