使用fastjson对java的Arraylist对象与 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{


	//将对象转化成json String
	public String getJson(ArrayList 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 getObj(String jsonstr,T t){
		ArrayList obj= (ArrayList) JSONObject.parseArray(jsonstr, t.getClass());
		return obj;
	}

}

你可能感兴趣的:(Java基础篇)