json 添加 和删除两种方法

<script>
var test = { name: "name", age: "12" };
var countrys = {
"newval": [{ "Country_code": "101", "Country_name": "中国" },
{ "Country_code": "102", "Country_name": "美国" }]
};
$(function () {
test.id = "1245";
alert(test.id);

//添加
var c = { "Country_code": "103", "Country_name": "英国" };
countrys.newval.push(c)
alert(countrys.newval[0].Country_name)
//删除
delete countrys.newval[1];
alert(countrys.newval[0].Country_name)
})
</script>

var str1 = {"name": "apple", "sex": "21"};

// 参数:prop = 属性,val = 值
function createJson(prop, val) {
// 如果 val 被忽略
if(typeof val === "undefined") {
// 删除属性
delete str1[prop];
}
else {
// 添加 或 修改
str1[prop] = val;
}
}

原文地址:https://www.cnblogs.com/yangpeng-jingjing/p/6729075.html