初识 JSON与JSON 的 3 种形式
初识 JSON
1.JSON是什么
Ajax 发送和接收书数据的一种格式
XML
username=alex&age=18
JSON
Json 全称是JavaScript Object Notation
2.为什么需要JSON
JSON有3中形式,每种形式的写法都和JS中的数据类型很像,可以很轻松的和JS中的数据类型互相转换
JS->JSON->PHP/Java
PHP/Java->JSON->JS
JSON 的 3 种形式
1.简单值形式
JSON的简单形式就对应着JS中的基础数据类型
数字、字符串、布尔值、null
注意:
JSON中没有undefined 值
JSON中的字符串必须使用双引号
JSON中是不能注释的
2. 对象形式
JSON的对象形式就对应着JS中的对象
注意事项
JSON中对象的属性名必须使用双引号,属性值如果是字符串也必须使用双引号
JSON 中只要涉及到字符串,就必须使用双引号
不支持undefined
3.数组形式
JSON的数组形式就对应着JS中的数组
注意
数组中的字符串必须用双引号
JSON只要涉及到字符串,就必须使用双引号
不支持undefined
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>JSON 的 3 种形式</title> </head> <body> <script> /* const xhr =new XMLHttpRequest(); xhr.onreadystatechange = () =>{ if(xhr.readyState!== 4) return; if(xhr.status>= 200&&xhr.status < 300 ||xhr.status===304){ console.log(xhr.responseText); console.log(typeof xhr.responseText) } }; xhr.open('GET','./json/plain.json',true); xhr.send(null); */ /* const xhr =new XMLHttpRequest(); xhr.onreadystatechange = () =>{ if(xhr.readyState!== 4) return; if(xhr.status>= 200&&xhr.status < 300 ||xhr.status===304){ console.log(xhr.responseText); console.log(typeof xhr.responseText) } }; //xhr.open('GET','./json/plain.json',true); xhr.open('GET','./json/ojb.json',true); xhr.send(null); */ const xhr =new XMLHttpRequest(); xhr.onreadystatechange = () =>{ if(xhr.readyState!== 4) return; if(xhr.status>= 200&&xhr.status < 300 ||xhr.status===304){ console.log(xhr.responseText); console.log(typeof xhr.responseText) } }; //xhr.open('GET','./json/plain.json',true); //xhr.open('GET','./json/ojb.json',true); xhr.open('GET','./json/arr.json',true); xhr.send(null); //1,"Loveweiwei",null </script> </body> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)