标签:集合 转换 gif style ast java google ToJson arraylist
一、JSON语法:
1.数组:[1,2,3];//数组里面可以放对象;
2.对象:{"key":value,"key":value};//对象里面可以放数组;
二、fastjson解析:
1.导包:fastjson.jar;
2.解析:
(1)序列化:String toJSONString(Object object);
(2)反序列化:
1.普通对象:
1)class<?> JSON.parseObject(String, class<?>.class);
//Student g = JSON.parseObject(序列化值, Student.class);
2)List<Object> JSON.parseArray(String, Object.class)
2.Lsit,set集合:
String json = "[{\"id\":\"1\",\"name\":\"Json技术\"},{\"id\":\"2\",\"name\":\"java技术\"}]";
1)将json转换成List:
List list = JSON.parseObject(json,new TypeReference<ARRAYLIST>(){});
2)将json转换成Set:
Set set = JSON.parseObject(json,new TypeReference<HASHSET>(){});
三、Gson解析:
1.导包:google-gson.jar;
2.解析:
(1)序列化:
Gson gson = new Gson();
String json = gson.toJson(obj);
(2)反序列化:
1)普通对象
Gson gson = new Gson();
String json = "{\"id\":\"2\",\"name\":\"Json技术\"}";
Book book = gson.fromJson(json, Book.class);
2)Lsit,set集合
Gson gson = new Gson();
String json = "[{\"id\":\"1\",\"name\":\"Json技术\"},{\"id\":\"2\",\"name\":\"java技术\"}]";
//将json转换成List
List list = gson.fromJson(json,new TypeToken<LIST>() {}.getType());
//将json转换成Set
Set set = gson.fromJson(json,new TypeToken<SET>() {}.getType());
三、js序列化/反序列化:
1.序列化:JSON.stringify(obj[arr]);//把对象或数组转为String;
2.反序列化:JSON.parse(字符串); //把字符串转换为javascript对象;
//非标字符串:使用eval("("+字符串+")");
3.json字符串:
1)标准:var str = ‘{"sid":"001","sname":"张三","sage":17,"ssex":"男"}‘;
//JSON.parse(str);
2)非标:var str = "{‘sid‘:‘001‘,‘sname‘:‘张三‘,‘sage‘:17,‘ssex‘:‘男‘}";
//eval("("+str+")");
标签:集合 转换 gif style ast java google ToJson arraylist
原文地址:https://www.cnblogs.com/Tractors/p/11290228.html