json与for in、json的对象的删除和其属性值得删除

xiaoxiao2021-02-28  112

 for in 遍历:(可以获取对象里面的所有属性和值)  for(var key in document){          console.log(key+”===”+document[key]);  } key是字符串, 案例: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>json for in遍历</title> <style type="text/css"> *{margin: 0;padding: 0;} #box{width: 200px;height: 200px;background: red;text-align: center;line-height: 100px;} </style> </head> <body> <div id="box"> hello! </div> <script type="text/javascript"> var oBox = $("box"); function $(id){ return document.getElementById(id); }; function css(json,obj){ for(var key in json){ obj.style[key] = json[key]; } }; css({ width:"250px", height:"300px", border:"9px solid green" },oBox); </script> </body> </html> json删除对象和删除属性 删除对象就是把json清空即可, json = null; 删除属性值, delete  json. name ; 检验json对象是否存在 , 用 in,返回false表示不存在 把对象转换为字符串, JSON.stringify(json)。反之,JSON.parse(str); var str =  "{\"$keywordIndex{19}\":\"Kery\",\"age\":18}" ; str = JSON.parse(str); str = eval( "(" +str+ ")" ); eval函数获取json对象。
转载请注明原文地址: https://www.6miu.com/read-31935.html

最新回复(0)