JSON是前後两端传输数据的一种比较轻量的格式
JSON分为两种结构:对象、数组
{'key1':value1,'key2':value2,'key3':value3}
[a,b,c]
var jsonText='{"employees":[{"name":"小A","birth":"2021-09-24"},\
{"name":"小B","birth":"2021-08-24"}]}'
var jsonObj = JSON.parse(jsonText,(key,value)=>{
if(key=="birth"){
return new Date(value);
}
else{
return value
}
})
console.log(jsonObj.employees)
console.log(jsonObj.employees[1].birth)
下面使用一下之前讲过的 arrow function,让程序更简洁
var jsonObj = JSON.parse(jsonText,(key,value)=>{if(key=="birth")return new Date(value);else return value})
""
括起来\
else return value
,没写会出错der<p id="demo" style="font-size: 30px;">demo</p>
<script>
//整个object框起来当作字串
var jsonText='{"employees":[{"name":"小A","age":20},{"name":"小B","age":21},{"name":"小C","age":22}],\
"boss":[{"name":"大A","age":30},{"name":"大B","age":31}]}';
//反斜线是为了换行
jobj=JSON.parse(jsonText);
console.log(jobj);
console.log(jobj.employees);
console.log(jobj.boss);
for(let i=0;i<jobj.boss.length;i++){
console.log(jobj.boss[i].name);
}
var demo =document.getElementById("demo");
var frag=0;
demo.onmouseover =()=>{
demo.style.color="";
if(frag==0){
demo.innerHTML=jobj.boss[0].name;
frag=1;
}else if(frag==1){
demo.innerHTML = jobj.boss[1].name;
frag=0;
}
}
demo.onmouseleave =()=>{
demo.innerHTML="不告诉你 boss是谁"
demo.style.color="pink";
}
</script>
<<: 【DAY 10】SharePoint 的应用五花八门,什麽最适合你?(下)
>>: 从 IT 技术面细说 Search Console 的 27 组数字 KPI (24) :检索统计报表 KPI 外的重点项目
在 APCS 历届考试下来,能够拿到五级分的人数仅占所有考生的 1~2%。光是程序设计能力好是不够的...
D: cd pydata call C:\ProgramData\Anaconda3\Scripts...
如昨天预告的一样,今天来介绍元件化开发的技术背景,它是什麽、为什麽重要,最後再讲一下元件的开发流程...
前言 在开始写Android APP前 必须先建置开发Android的环境 在这边选择被广为使用的A...
前言: 本篇文章内容注重在把照相照片/相簿照片转成上传至Server的 Multipart.Part...