概念
javascript object notation js标示对象的一种方式
作用
1、标示数据,键值对形式 2、保存数据 3、json可以作为数据交换的格式
特点
轻量级,原生js
json对象 本质就是js代码 var aa={"name":"lisu"}
json数据 字符串
eval();
json数据
map形式
1、key/value
{"name":"zhangsan","age":"30"};
2、object
{"stu":{"name":"lisi","age":"21"}}
3、array
{"tea":[
{"name":"lisi","age":"21"},
{"name":"zhangsan","age":"22"}
]
}
4、复杂嵌套
{
"stu2":[
{"sex":0,"username":"lisi"},{"sex":0,"username":"zhaoliu"},
{"password":"123","sex":0,"username":"zhangsan"}
],
"stu1":{"password":"123","sex":1,"username":"zhangsan"}
}
list形式
[
{"name":"lisi","age":"21"},
{"name":"zhangsan","age":"22"}
]
[
[
{"name":"lisi","age":"21"},
{"name":"zhangsan","age":"22"}
],
[
{"name":"lisi","age":"21"},
{"name":"zhangsan","age":"22"}
]
]
json工具类
访问官网http://www.json.org/
找到java平台的json工具类,例如json-java
到github下载源码
导入到eclipse,导出jar
json-java的使用
javabean对象转成json对象
User user=new User("zhaoliu", "123");
JSONObject json=new JSONObject(user);//将user对象转成json对象
list对象转成json对象
User user1=new User("lisi","123");
User user2=new User("zhaoliu","123");
User user3=new User("zhangsan","123");
List<User> list=new ArrayList<User>();
list.add(user1);
list.add(user2);
list.add(user3);
JSONArray json=new JSONArray(list);//将list<User>转成json对象