使用fastjson对java的Arraylist对象与 jsonstr之间的互转,fastjsonjsonstr
分享于 点击 35017 次 点评:264
使用fastjson对java的Arraylist对象与 jsonstr之间的互转,fastjsonjsonstr
package com.dl.utils;
import java.util.ArrayList;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class MyJsonObject<T>{
//将对象转化成json String
public String getJson(ArrayList<T> t){
ObjectMapper mapper = new ObjectMapper();
String jsonstr = null;
try {
jsonstr = mapper.writeValueAsString(t);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return jsonstr;
}
//Arralist json转化为 Arraylist 对象
public ArrayList<T> getObj(String jsonstr,T t){
ArrayList<T> obj= (ArrayList<T>) JSONObject.parseArray(jsonstr, t.getClass());
return obj;
}
}
相关文章
- 暂无相关文章
用户点评