JavaScript 之 JSON [2] 对象和数组的定义、访问、修改
JavaScript 之 JSON [2] 对象和数组的定义、访问、修改
1、对象和数组的定义、访问、修改
var JSONObject,Jvale;
JSONObject= { //对象
"name":"滔Roy",
"date":"2023.04.14",
"other":[12,"TaoRoy",null,true] //数组
};
Jvale=JSONObject.name; //对象的访问
document.getElementById("demo").innerHTML = Jvale ;
JSONObject.name="TaoRoy"; //对象的修改
Jvale = Jvale + "<br>" + JSONObject.name;
document.getElementById("demo").innerHTML = Jvale;
Jvale = Jvale + "<br>" + JSONObject.other;
document.getElementById("demo").innerHTML = Jvale;
JSONObject.other[2] = 2023 //数组的修改
Jvale = Jvale + "<br>" + JSONObject.other;
document.getElementById("demo").innerHTML = Jvale;
2、循环获取
//循环获取数据
for (let key in JSONObject) {
Jvale = Jvale + "<br>" + key + ": " + JSONObject[key];
document.getElementById("demo").innerHTML = Jvale;
}
创建时间:2023.04.14 更新时间:
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!