js操作json

<script type="text/javascript">
    var json = {
      "age":24,
      "name":"cst"
    };
    //修改Json中的age值,因为Json中存在age属性
    json["age"] = 30;
    alert(json.age); //30
 
    //增加Json中的sex值,因为Json中不存在sex属性
    json["sex"] = "M";
    alert(json.sex); //M
 
    <!-- 遍历Json中的数据 -->
    for(var key in json){
      try{
        var value = eval("json['" + key +"']");
        alert(key+"_"+value);
      }catch(e){}
    }
 
    //删除Json数据中的age属性
    delete json["age"];
    alert(json.age); //undefined
 
  </script>

原文地址:https://www.cnblogs.com/aipan/p/8361324.html