FastJSON进⾏对象的序列化和反序列化Java中使⽤FastJSON进⾏对象的序列化和反序列化
1.添加依赖,maven的l⽂件中添加以下依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
2.FastJSON序列化和反序列化⼯具类
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/
/ JSON/对象转换类
public class JsonUtils {
/**
* JSON字符串转换成对象
*/
public static <T> T jsonStringToObject(String jsonStr,Class<T> obj){
try{
return JSONObject.parseObject(jsonStr, obj);
}catch(Exception e){
System.out.println("将JSON串{}转换成指定对象失败:"+jsonStr);
e.printStackTrace();
}
return null;
}
/**
* 对象转JSON
*/
public static <T> String objectToJson(T obj){
try{
String JSONString(obj);
return json;
}catch(Exception e){
System.out.println("将指定对象转成JSON串{}失败:"+String());
e.printStackTrace();
}
return "";
}
/**
* List<T>对象转成json
*/
public static<T> String listToJsonString(List<T> objList){
try{
String JSONString(objList);
return json;
}catch(Exception e){
System.out.println("将对象列表转成JSON串{}失败:"+String());
e.printStackTrace();
}
return "";
}
/**
* json转换成对象列表List<T>
*/
public static <T> List<T> jsonStringToList(String json,Class<T> obj){
try{
return JSONArray.parseArray(json, obj);
}catch(Exception e){
System.out.println("将JSON串{}转成对象列表失败:"+json);
e.printStackTrace();
}
return null;
}
/*
* 将JSON串转换为JSONOBJECT
*/
public static JSONObject stringTOJSONObject(String json){
JSONObject jsonObject = null;
try {
jsonObject = JSONObject.parseObject(json);
} catch (Exception e) {
System.out.println("JSON串{} 转换成 JsonObject失败"+json);
}
return jsonObject;
}
}
3.使⽤
(1)序列化
List<User> users=new ArrayList<User>();
for(int i=0;i<20;i++){
User user=new User();
user.setName("FastJSON"+i);
user.setAge(20+i);
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");        user.setBirthday(dateformat.parse("1991-10-01"));
user.setEmail("12345678"+i+"@qq");
// 序列化单个对象
String json=JsonUtils.objectToJson(user);
users.add(user);
// 序列化对象列表
// String json=JsonUtils.listToJsonString(users);
}
(2)反序列化
// 反序列化单个对象
User u=JsonUtils.jsonStringToObject(json, User.class);
System.out.String());
// 将JSON串转换为JSONOBJECT
JSONObject js=JsonUtils.stringTOJSONObject(json);
System.out.println(js);
// 反序列化对象列表
List<User> list=JsonUtils.jsonStringToList(json, User.class);
System.out.println(list);
4.FastJson的常见操作
(1)⽇期进⾏格式化
String JSONString(new Date(), SerializerFeature.WriteDateUseDateFormat);    System.out.println("使⽤JSON中的SerializerFeature特性格式化⽇期:"+dateToJson);
String JSONStringWithDateFormat(new Date(), "yyyy/MM/dd");
System.out.println("JSON中⾃定义⽇期的输出格式:"+dateToJson2);
(2)将Map转换成JSONObject然后添加元素,最后输出。
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "Gelpe");
map.put("age", 30);
map.put("brithday",dateToJson);
json转换对象map.put("date",dateToJson2);
// 将Map对象转成JSON字符串
String JSONString(map);
System.out.println("将MAP转成JSON: :"+mapToJson);
// 将Map转换成JSONObject对象
JSONObject jsonObject = new JSONObject(map);
System.out.println("JSONObject 1:"+jsonObject);
(3)List转成JSON
List<String> list=new ArrayList<String>();
list.add("shopping");
list.add("travel");
String JSONString(list);
System.out.println("将List中存储的字符串转成JSON: :"+listToJson);
(4)向JSONObject中添加值
jsonObject.put("hobbies",list);
(5)从JSONObject中取值
System.out.("hobbies"));
System.out.println("JSONObject 2:"+jsonObject);
(6)将List对象转成JSONArray,然后输出
List<Map<String,Object>> mapList=new ArrayList<Map<String,Object>>();
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("address", "Beijing");
map3.put("city", "Beijing");
Map<String, Object> map4 = new HashMap<String, Object>();
map4.put("town", "wulukou");
map4.put("street", "Four street");
mapList.add(map3);
mapList.add(map4);
String JSONString(mapList);
System.out.println("将List中存储的MAP对象转成JSON:"+listMapToJson);
(7)JSON格式化输出
// 第⼆个参数true表⽰是否格式化输出
String JSONString(mapList, true);
System.out.println("将List中存储的MAP对象转成JSON并且格式化输出:"+listMapToJson2);
(8)将List对象转成JSONArray
JSONArray jsonArray=JSONArray.JSONString(mapList));
System.out.println("JSONArray:"+jsonArray);
(9)打印JSONArray中的内容
for(int i=0;i<jsonArray.size();i++){
System.out.println("JSONArray Content:"+(i));
}
(10)将JSONArray放到JSONObject中
jsonObject.put("addressInfo",jsonArray);
System.out.println("JSONObject 3:"+jsonObject);
(11)⾃定义对象转成JSON
User u=new User();
u.setName("NB Person");
u.setAge(24);
u.setBirthday(new Date());
// ⾃定义对象转成JSON
String JSONString(u);
System.out.println("⾃定义对象转成JSON:"+userJson);
以上操作,输出的内容:
使⽤JSON中的SerializerFeature特性格式化⽇期:"2018-12-01 10:58:46"
JSON中⾃定义⽇期的输出格式:"2018/12/01"
将MAP转成JSON: :{"date":"\"2018/12/01\"","brithday":"\"2018-12-01 10:58:46\"","name":"Gelpe","age":30}
JSONObject 1:{"date":"\"2018/12/01\"","brithday":"\"2018-12-01 10:58:46\"","name":"Gelpe","age":30}
将List中存储的字符串转成JSON: :["shopping","travel"]
[shopping, travel]
JSONObject 2:{"date":"\"2018/12/01\"","hobbies":["shopping","travel"],"brithday":"\"2018-12-01 10:58:46\"","name":"Gelpe","age":30}
将List中存储的MAP对象转成JSON:[{"address":"Beijing","city":"Beijing"},{"town":"wulukou","street":"Four street"}]
将List中存储的MAP对象转成JSON并且格式化输出:[
{
"address":"Beijing",
"city":"Beijing"
},
{
"town":"wulukou",
"street":"Four street"
}
]
JSONArray:[{"address":"Beijing","city":"Beijing"},{"town":"wulukou","street":"Four street"}]
JSONArray Content:{"address":"Beijing","city":"Beijing"}
JSONArray Content:{"town":"wulukou","street":"Four street"}
JSONObject 3:{"date":"\"2018/12/01\"","addressInfo":[{"address":"Beijing","city":"Beijing"},{"town":"wulukou","street":"Four street"}],"hobbies":["shopping","travel"],"b ⾃定义对象转成JSON:{"age":24,"birthday":1543648393681,"name":"NB Person"}
JSON的解析:
(1)集合反序列化
Map<String,String> map=JSON.parseObject(jsonString, Map.class);
System.out.println("map get name:"+("name"));
System.out.println("map:"+map);
(2)泛型的反序列化,使⽤TypeReference传⼊类型信息
Map<String,String> map2=JSON.parseObject((jsonString,new TypeReference<Map<String,String>>(){});
System.out.println("map2 get name:"+("name"));
System.out.println("map2 get hobbies:"+("hobbies"));
System.out.println("map2:"+map2);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。