json中的数据类型分为:数字、字符串、布尔、null、数组和对象

用 [] 括起来的是数组,用 { } 括起来的都是对象

声明一个对象如下:var user={"userId":"1001",“username”:"Lucy","password":"123456","userAge":18};

声明一个数组如下:var usernameArr=["Lucy","Lily","Tom",“Lilei”,"Hanmeimei"];

声明一个数组对象如下:var userArrObject={"userVO":[

{"userId":1,“username”:"Lucy","password":"123456"},

{"userId":2,“username”:"Lily",“password”:"1234567"},

]}


测试代码如下:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>json测试</title>
  <script type="text/javascript">
		//1.json文本格式
		var user="{'userId':'11','username':'Tom','userAge':'18','password':'123456'}";
		//输出的都是undefined,不知道类型
		document.write(user.userId+" "+user.username);
		//使用eval将json文本格式数据解析为json对象
		var userObj=eval("("+ user+")");
		document.write(userObj.userId+" "+userObj.username);
		
		//2.json数组对象
		var emps={"employees":[
			{"empId":"1","empName":"Tom"},
			{"empId":"2","empName":"Lilei"},
			{"empId":"3","empName":"Lucy"},
			{"empId":"4","empName":"Lili"},
			{"empId":"5","empName":"Hanmeimei"},
		]};
		document.write(emps.employees[0].empId+"---"+emps.employees[0].empName);
		
		var emp='{"id":"1001","empName":"Lucy","hobbit":["Lol","basketBall"]}';
		var empObj=eval("("+ emp +")");
		alert(empObj.hobbit[0]+" "+empObj.hobbit[1]);
  </script>
 </head>
 <body>
  
 </body>
</html>


GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐