fastjson 与jackson 工具类封装

前端之家收集整理的这篇文章主要介绍了fastjson 与jackson 工具类封装前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package util.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
@SuppressWarnings("all")
public class FastJsonUtil {
	private static final Logger logger = LoggerFactory
			.getLogger(FastJsonUtil.class);

	public static final <T> List<T> getList(String jsontext,String list_str,Class<T> clazz) {
		JSONObject jsonobj = JSON.parSEObject(jsontext);
		if (jsonobj == null) {
			return null;
		}
		Object obj = jsonobj.get(list_str);
		if (obj == null) {
			return null;
		}
		// if(obj instanceof JSONObject){}
		if (obj instanceof JSONArray) {
			JSONArray jsonarr = (JSONArray) obj;
			List<T> list = new ArrayList<T>();
			for (int i = 0; i < jsonarr.size(); i++) {
				list.add(jsonarr.getObject(i,clazz));
			}
			return list;
		}
		return null;
	}

	/**
	 * @param <T>
	 *            -> DepartmentBean
	 * @param jsontext
	 *            -> {"department":{"id":"1","name":"生产部"},"password":"admin",*            "username":"admin"}
	 * @param obj_str
	 *            -> department
	 * @param clazz
	 *            -> DepartmentBean
	 * @return -> T
	 */
	public static final <T> T getObject(String jsontext,String obj_str,Class<T> clazz) {
		JSONObject jsonobj = JSON.parSEObject(jsontext);
		if (jsonobj == null) {
			return null;
		}

		Object obj = jsonobj.get(obj_str);
		if (obj == null) {
			return null;
		}

		if (obj instanceof JSONObject) {
			return jsonobj.getObject(obj_str,clazz);
		} else {
			logger.info(obj.getClass()+"");
		}

		return null;
	}

	/**
	 * @param <T>
	 * @param jsontext
	 *            ->{"department":{"id":"1",*            "username":"admin"}
	 * @param clazz
	 *            -> UserBean.class
	 * @return -> UserBean
	 */
	// 注:传入任意的jsontext,返回的T都不会为null,只是T的属性为null
	public static final <T> T getObject(String jsontext,Class<T> clazz) {
		T t = null;
		try {
			t = JSON.parSEObject(jsontext,clazz);
		} catch (Exception e) {
			logger.error("json字符串转换失败!" + jsontext,e);
		}
		return t;
	}

	public static final String toJSONString(Object object,boolean prettyFormat) {
		return JSON.toJSONString(object,prettyFormat);
	}

	/**
	 * @Description: json字符串转成为List
	 * @author: GuXiYang
	 * @date: 2017/05/08 10:25:00
	 * @param jsonStr
	 *            json字符串
	 * @param clazz
	 *            class名称
	 * @return
	 */
	public static <T> List<T> getList(String jsonStr,Class<T> clazz) {
		List<T> list = new ArrayList<T>();
		try {
			list = JSON.parseArray(jsonStr,clazz);
		} catch (Exception e) {
			logger.error("json字符串转List失败!" + jsonStr,e);
		}
		return list;
	}

	/**
	 * 
	 * @Description: json字符串转换成list<Map>
	 * @author: GuXiYang
	 * @date: 2017/05/08 10:27:16
	 * @param jsonString
	 *            json字符串
	 * @return
	 */
	public static List<Map<String,Object>> listKeyMaps(String jsonString) {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		try {
			list = JSON.parSEObject(jsonString,new TypeReference<List<Map<String,Object>>>() {
			});
		} catch (Exception e) {
			logger.error("json字符串转map失败",e);
		}
		return list;
	}

	/**
	 * @Description: json字符串转换为Map
	 * @author: GuXiYang
	 * @date: 2017/05/08 10:32:33
	 * @param jsonStr
	 *            json字符串
	 * @return
	 */
	public static Map<String,Object> json2Map(String jsonStr) {
		try {
			return JSON.parSEObject(jsonStr,Map.class);
		} catch (Exception e) {
			logger.error("json字符串转换失败!" + jsonStr,e);
		}
		return null;
	}

	
//	public static void main(String[] args) {
//		Map<String,Object> dataMap = new HashMap<String,Object>();
//		dataMap.put("tag","data");
//		dataMap.put("hospitalName","宜都市妇幼保健院");
//		dataMap.put("name","欧阳夏凡");
//		dataMap.put("gender","女");
//		dataMap.put("age","28");
//		dataMap.put("code","420502042");
//		dataMap.put("examineDoc","杨林");
//		dataMap.put("examineDate","2016-05-10");
//		dataMap.put("verifyDoc","王菲");
//		String jsonString=JSON.toJSONString(dataMap);
//		System.out.println(jsonString);
//       Map object = getObject(jsonString,Map.class);
//       System.out.println(object.get("age"));
//	}
}


package org.ph.system.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Created by phshy on 2017/1/19.
 */
public class JSONUtils {  
    private static final ObjectMapper mapper = new ObjectMapper();

    private JSONUtils() {
    }

    public static Map<String,Object> json2Map(String json) throws IOException {
        Map map = (Map) mapper.readValue(json,Map.class);
        return map;
    }

    public static String object2Json(Object object) throws IOException {
        return mapper.writeValueAsString(object);
    }

    public static <T> T json2Object(String json,Class<T> clazz) throws IOException {
        return mapper.readValue(json,clazz);
    }

    public static List<?> json2List(String json,TypeReference<?> typeRef) throws IOException {
        List list = (List) mapper.readValue(json,typeRef);
        return list;
    }
     
    public static void main(String[] args) throws IOException {
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		
		Map<String,Object>();
		dataMap.put("tag","data");
		dataMap.put("hospitalName","宜都市妇幼保健院");
		dataMap.put("name","欧阳夏凡");
		dataMap.put("gender","女");
		dataMap.put("age","28");
		dataMap.put("code","420502042");
		dataMap.put("examineDoc","杨林");
		dataMap.put("examineDate","2016-05-10");
		dataMap.put("verifyDoc","王菲");
		

		Map<String,Object> dataMap2 = new HashMap<String,Object>();
		dataMap2.put("no","340812");
		dataMap2.put("name","古夕旸");
		 
//		
		String jsonString=JSON.toJSONString(dataMap);
		list.add(dataMap);
		list.add(dataMap2);
//		String jsonString2=mapper.writeValueAsString(list);
		String jsonString2=JSON.toJSONString(list);
		System.out.println(jsonString2);
		
		
		List<Map<String,Object>> json2List = (List<Map<String,Object>>) json2List(jsonString2,Object>>>(){});
		for (Map<String,Object> s:json2List) {
			System.out.println(s.get("name"));
		}
		
		
	}
}
原文链接:https://www.f2er.com/json/288927.html

猜你在找的Json相关文章