json
概念:javascript object notation js标示对象的一种方式
作用: 1、标示数据,键值对形式 2、保存数据 3、json可以作为数据交换的格式
特点:轻量级,原生js
json对象 本质就是js代码 var aa={"name":"lisu"}
json数据 字符串
eval();
json数据
Map形式
- key/value
{"name":"zhangsan","age":"30"};
- object
{"stu":{"name":"lisi","age":"21"}}
- array
{"tea":[
{"name":"lisi","age":"21"},
{"name":"zhangsan","age":"22"}
]
}
- 复杂嵌套
{
"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工具类
找到java平台的json工具类,例如json-java
到github下载源码
导入到eclipse,导出jar
json-java的使用
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对象