赞
踩
1、json循环遍历
var jsons1 = {"name":"grace","age":23,"live":"beijing"};
for(var m in jsons1){
console.log(m);
}
打印处理的结果如下图所示:
如果需要的是每个属性名对应的值,打印出console.log(jsons1[m]);即可,结果如下
2. json增加新的属性或者修改/删除某属性
(1)增加
var json={"test":"aa"};
json["name"] = "wangxy";
得到{"test":"aa","name":wangxy"};
其中,name也可以是一个变量名,例如:var name=“wang”;
json[name] = "xiaoyu";
得到{"test":"aa","wang":"xiaoyu"};
(2) 修改
可以通过循环批量修改
var val = 0;
var jsons1 = {"name":"grace","age":23,"live":"beijing"};
for(var m in jsons1){
var data = jsons1[m];
data = val++;
jsons1[m] = data;
}
console.log(jsons1);
得到 {name: 0, age: 1, live: 2};
var jsons1 = {"name":"grace","age":23,"live":"beijing"};
单个修改:jsons1.name="ji";
得到: {"name":"ji","age":23,"live":"beijing"};
删除
delete jsons1["age"];
删除之后 再取该属性值就是undefined
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。