使用fastjson对java的Arraylist<T>对象与 jsonstr之间的互转

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;
	}

}

猜你喜欢

转载自blog.csdn.net/ppwwp/article/details/82972267