{"student":[
{"name":"张三","address":"河南","age":20},
{"name":"李四","address":"北京","age":25},
{"name":"王五","address":"上海","age":30}
]}
student 是一个对象,里边有三条对象的信息
“name”:“张三” 等同于 JavaScript的 name=“张三”
// javascript 语法,等同于上述 json 案例
var student = [
{"name":"张三","address":"河南","age":20},
{"name":"李四","address":"北京","age":25},
{"name":"王五","address":"上海","age":30}
];
// 找到张三
student[0].name;
<student>
<s>
<name>张三name> <address>河南address> <age>20age>
s>
<s>
<name>李四name> <address>北京address> <age>25age>
s>
<s>
<name>王五name> <address>上海address> <age>30age>
s>
student>
方式一:使用 “.”
var student,username;
student = {"name":"张三","address":"河南","age":30};
username = student.name;
方式二:使用 “[]”
var student,username;
student = {"name":"张三","address":"河南","age":30};
username = student["name"];
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p id="demo">p>
<script>
var student = {"name":"张三","address":"河南","age":30};
for(x in student){
document.getElementById("demo").innerHTML += x + "--" + student[x] + "
";
}
script>
body>
html>
实例
student{
"name":"张三",
"address":"河南",
"technologys":{
"t1":"Java",
"t2":"Python",
"t3":"Go"
}
}
访问
x = student.technologys.t1;
// 或
x = student.technologys["t1"];
使用 delete 关键字来删除 JSON 对象的属性
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p id="demo">p>
<script>
var student = {
"name":"张三",
"address":"河南",
"technologys":{
"t1":"Java",
"t2":"Python",
"t3":"Go"
}
}
delete student.technologys.t3;
for(x in student.technologys){
document.getElementById("demo").innerHTML += x + "--" + student.technologys[x] + "
";
}
script>
body>
html>
JSON 通常用于与服务端交换数据,在接收服务器数据时一般是字符串
语法
JSON.parse(text[,reviver])
text:必需,一个有效的 JSON 字符串
reviver:可选,一个转换结果的函数,将为对象的每个成员调用此函数
示例代码
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p id="demo">p>
<script>
var obj = JSON.parse('{"student":[\n' +
' {"name":"张三","address":"河南","age":20},\n' +
' {"name":"李四","address":"北京","age":25},\n' +
' {"name":"王五","address":"上海","age":30}\n' +
']}');
document.getElementById("demo").innerHTML = obj.student[0].name + ":" + obj.student[0].address;
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p id="demo">p>
<script>
var text = '{"name":"张三","age":30,"address":"北京","createDate":"2024-01-01"}';
var obj = JSON.parse(text,function (key, value){
if(key == "createDate"){
return new Date(value);
} else{
return value;
}
});
document.getElementById("demo").innerHTML = obj.name + "创建日期:" + obj.createDate;
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p id="demo">p>
<script>
var text = '{"name":"清华大学","rank":"function(){return 1}","address":"北京"}';
var obj = JSON.parse(text);
// 字符串转函数
obj.rank = eval("("+ obj.rank +")");
document.getElementById("demo").innerHTML = obj.name + "学校排名:" + obj.rank();
script>
body>
html>
注意:将 函数作为字符串存储,然后再将字符串转换为函数
JSON 通常用于与服务端交换数据,在发送服务器数据时一般是字符串
语法
JSON.stringifg(value[,replacer[,space]]);
value:必需,要转换的 JavaScript 值(通常为对象或数组)
replacer:可选,用于转换结果的函数或数组
space:可选,文本添加缩进、空格和换行符,space是数字,指定数目的空格
示例代码,对象转换
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p id="demo">p>
<script>
var obj = {"name":"张三","address":"河南","age":30};
var s = JSON.stringify(obj);
document.getElementById("demo").innerHTML = s;
script>
body>
html>
示例代码,数组转换
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p id="demo">p>
<script>
var arr = [
{"name":"张三","address":"河南","age":30},
{"name":"李四","address":"湖北","age":35}
];
var s = JSON.stringify(arr);
document.getElementById("demo").innerHTML = s;
script>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p id="demo">p>
<script>
// 创建一个对象
var obj = {"name":"小哼","createDate":new Date(),"age":25};
// 对象转字符串
var s = JSON.stringify(obj);
document.getElementById("demo").innerHTML = s;
script>
body>
html>
注意:将对象转换成了字符串,可以利用parse再转成对象
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p id="demo">p>
<script>
var obj = {"name":"小哼","age":function (){return 25}};
// 函数转换字符串
obj.age = obj.age.toString();
var s = JSON.stringify(obj);
document.getElementById("demo").innerHTML = s;
script>
body>
html>
注意:函数转换字符串-使用 toString() 方法
概述:eval() 函数可将 JSON 文本转换成 JavaScript 对象
语法
var obj = eval("(" + text + ")");
示例代码
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<p id="demo">p>
<script>
// JSON 文本
var text = '{"student":[\n' +
' {"name":"张三","address":"河南","age":20},\n' +
' {"name":"李四","address":"北京","age":25},\n' +
' {"name":"王五","address":"上海","age":30}\n' +
']}';
// 调用 eval 函数,将 JSON 字符串转换成 JavaScript 对象
var obj = eval("("+ text +")");
document.getElementById("demo").innerHTML += obj.student[0].name;
document.getElementById("demo").innerHTML += obj.student[1].address;
script>
body>
html>