1:动态添加 对象属性
var obj = new Object();
console.log (obj.username);
obj.username = "haha";
console.log (obj.username);
//undefined
//haha
用“[]”表示。写成 obj["username"] = "haha";
var obj = new Object();
console.log (obj.username);
obj["username"] = "haha";
console.log (obj.username);
//undefined
//haha
还可以直接定义
var obj = {username:"haha", password:"123"};
console.log(obj.username);
console.log(obj.password);
//haha
//123
2:删除属性,使用 delete
var obj = new Object();
obj["username"] = "haha";
console.log (obj.username);
delete obj.username;
console.log (obj.username);
//haha
//undefined
3:修改原属性,增加新属性
var json = {
"age":24,
"name":"haha"
};
//修改Json中的age值
json["age"] = 30;
console.log(json.age);
//30
//增加Json中的sex值
json["sex"] = "w";
console.log(json.sex);
//w
4:Json对象与Json字符串互转
JSON.parse(jsonstr); //可以将json字符串转换成json对象
var jsObj = {};
jsObj.testArray = [1,2,3];
jsObj.name = \'CSS3\';
jsObj.date = \'2017\';
console.log(jsObj)
console.log(typeof(jsObj));
var str = JSON.stringify(jsObj);
var str1 = JSON.parse(str);
console.log(str)
console.log(typeof(str));
console.log(str1)
console.log(typeof(str1));
//Object {testArray: Array[3], name: "CSS3", date: "2017"}
//object
// {"testArray":[1,2,3],"name":"CSS3","date":"2017"}
// string
//Object {testArray: Array[3], name: "CSS3", date: "2017"}
// object
JSON.stringify(jsonobj); //可以将json对象转换成json字符串
var jsObj = {};
jsObj.testArray = [1,2,3];
jsObj.name = \'CSS3\';
jsObj.date = \'2017\';
console.log(jsObj)
console.log(typeof(jsObj));
var str = JSON.stringify(jsObj);
console.log(str)
console.log(typeof(str));
//Object {testArray: Array[3], name: "CSS3", date: "2017"}
// object
// {"testArray":[1,2,3],"name":"CSS3","date":"2017"}
//string