JSON和JAVA相互转换

Posted 八月长安个人专栏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON和JAVA相互转换相关的知识,希望对你有一定的参考价值。

java代码:

package util;

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

import org.apache.commons.beanutils.BeanUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * ClassName: JSONHelper <br/>
 * Function: JSON和JAVA的POJO的相互转换. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年6月4日 上午11:09:19 <br/>
 *
 * @author liuweiying
 * @version 
 * @since JDK 1.6
 */
public final class JSONHelper 
	private static final Logger logger = LoggerFactory.getLogger(JSONHelper.class);

	// 将数组转换成JSON
	public static String array2json(Object object) 
		JSONArray jsonArray = JSONArray.fromObject(object);
		return jsonArray.toString();
	

	// 将JSON转换成数组,其中valueClz为数组中存放的对象的Class
	public static Object json2Array(String json, Class valueClz) 
		JSONArray jsonArray = JSONArray.fromObject(json);
		return JSONArray.toArray(jsonArray, valueClz);
	

	// 将Collection转换成JSON
	public static String collection2json(Object object) 
		JSONArray jsonArray = JSONArray.fromObject(object);
		return jsonArray.toString();
	

	// 将Map转换成JSON
	public static String map2json(Object object) 
		JSONObject jsonObject = JSONObject.fromObject(object);
		return jsonObject.toString();
	

	// 将JSON转换成Map,其中valueClz为Map中value的Class,keyArray为Map的key
	public static Map json2Map(Object[] keyArray, String json, Class valueClz) 
		JSONObject jsonObject = JSONObject.fromObject(json);
		Map classMap = new HashMap();

		for (int i = 0; i < keyArray.length; i++) 
			classMap.put(keyArray[i], valueClz);
		

		return (Map) JSONObject.toBean(jsonObject, Map.class, classMap);
	

	// 将POJO转换成JSON
	public static String bean2json(Object object) 
		JSONObject jsonObject = JSONObject.fromObject(object);
		return jsonObject.toString();
	

	// 将JSON转换成POJO,其中beanClz为POJO的Class
	public static Object json2Object(String json, Class beanClz) 
		return JSONObject.toBean(JSONObject.fromObject(json), beanClz);
	

	/**
	 * json转换为java对象
	 * 
	 * <pre>
	 * return JackJson.fromJsonToObject(this.answersJson, JackJson.class);
	 * </pre>
	 * 
	 * @param <T>
	 *            要转换的对象
	 * @param json
	 *            字符串
	 * @param valueType
	 *            对象的class
	 * @return 返回对象
	 */
	public static <T> T fromJsonToObject(String json, Class<T> valueType) 
		ObjectMapper mapper = new ObjectMapper();
		try 
			return mapper.readValue(json, valueType);
		 catch (JsonParseException e) 
			logger.error("JsonParseException: ", e);
		 catch (JsonMappingException e) 
			logger.error("JsonMappingException: ", e);
		 catch (IOException e) 
			logger.error("IOException: ", e);
		
		return null;
	

	// 将String转换成JSON
	public static String string2json(String key, String value) 
		JSONObject object = new JSONObject();
		object.put(key, value);
		return object.toString();
	

	// 将JSON转换成String
	public static String json2String(String json, String key) 
		JSONObject jsonObject = JSONObject.fromObject(json);
		return jsonObject.get(key).toString();
	

	/***
	 * 将List对象序列化为JSON文本
	 */
	public static <T> String toJSONString(List<T> list) 
		JSONArray jsonArray = JSONArray.fromObject(list);

		return jsonArray.toString();
	

	/***
	 * 将对象序列化为JSON文本
	 * 
	 * @param object
	 * @return
	 */
	public static String toJSONString(Object object) 
		JSONArray jsonArray = JSONArray.fromObject(object);

		return jsonArray.toString();
	

	/***
	 * 将JSON对象数组序列化为JSON文本
	 * 
	 * @param jsonArray
	 * @return
	 */
	public static String toJSONString(JSONArray jsonArray) 
		return jsonArray.toString();
	

	/***
	 * 将JSON对象序列化为JSON文本
	 * 
	 * @param jsonObject
	 * @return
	 */
	public static String toJSONString(JSONObject jsonObject) 
		return jsonObject.toString();
	

	/***
	 * 将对象转换为List对象
	 * 
	 * @param object
	 * @return
	 */
	public static List toArrayList(Object object) 
		List arrayList = new ArrayList();

		JSONArray jsonArray = JSONArray.fromObject(object);

		Iterator it = jsonArray.iterator();
		while (it.hasNext()) 
			JSONObject jsonObject = (JSONObject) it.next();

			Iterator keys = jsonObject.keys();
			while (keys.hasNext()) 
				Object key = keys.next();
				Object value = jsonObject.get(key);
				arrayList.add(value);
			
		

		return arrayList;
	

	/* *//***
	 * 将对象转换为Collection对象
	 * 
	 * @param object
	 * @return
	 */
	/*
	 * public static Collection toCollection(Object object)  JSONArray
	 * jsonArray = JSONArray.fromObject(object);
	 * 
	 * return JSONArray.toCollection(jsonArray); 
	 */

	/***
	 * 将对象转换为JSON对象数组
	 * 
	 * @param object
	 * @return
	 */
	public static JSONArray toJSONArray(Object object) 
		return JSONArray.fromObject(object);
	

	/***
	 * 将对象转换为JSON对象
	 * 
	 * @param object
	 * @return
	 */
	public static JSONObject toJSONObject(Object object) 
		return JSONObject.fromObject(object);
	

	/***
	 * 将对象转换为HashMap
	 * 
	 * @param object
	 * @return
	 */
	public static HashMap toHashMap(Object object) 
		HashMap<String, Object> data = new HashMap<String, Object>();
		JSONObject jsonObject = JSONHelper.toJSONObject(object);
		Iterator it = jsonObject.keys();
		while (it.hasNext()) 
			String key = String.valueOf(it.next());
			Object value = jsonObject.get(key);
			data.put(key, value);
		

		return data;
	
	
	  /** 
	    * 将json格式的字符串解析成Map对象 <li> 
	    * json格式:"name":"admin","retries":"3fff","testname":"ddd","testretries":"fffffffff" 
	    */  
	   public static Map<String, Object> json2Map(String jsonStr)    
	       Map<String, Object> data = new HashMap<String, Object>();  
	       // 将json字符串转换成jsonObject  
	       JSONObject jsonObject = JSONObject.fromObject(jsonStr);  
	       Iterator it = jsonObject.keys();  
	       // 遍历jsonObject数据,添加到Map对象  
	       while (it.hasNext())  
	         
	           String key = String.valueOf(it.next());  
	           Object value = jsonObject.get(key);  
	           data.put(key, value);  
	         
	       return data;  
	     
	   
	   
	   /** 
	    * 将json格式的字符串解析成Map对象 <li> 
	    * json格式:"name":"admin","retries":"3fff","testname":"ddd","testretries":"fffffffff" 
	    */  
	   public static Map<String, List<Map<String, Object>>> json2MapList(String jsonStr)    
	       Map<String, List<Map<String, Object>>> data = new HashMap<String, List<Map<String, Object>>>();  
	       // 将json字符串转换成jsonObject  
	       JSONObject jsonObject = JSONObject.fromObject(jsonStr);  
	       Iterator it = jsonObject.keys();  
	       // 遍历jsonObject数据,添加到Map对象  
	       while (it.hasNext())  
	         
	           String key = String.valueOf(it.next());  
	           Object value = jsonObject.get(key);  
	           List<Map<String, Object>> list = toList(value);
	           data.put(key, list);  
	         
	       return data;  
	     

	/***
	 * 将对象转换为List<Map<String,Object>>
	 * 
	 * @param object
	 * @return
	 */
	// 返回非实体类型(Map<String,Object>)的List
	public static List<Map<String, Object>> toList(Object object) 
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		JSONArray jsonArray = JSONArray.fromObject(object);
		for (Object obj : jsonArray) 
			JSONObject jsonObject = (JSONObject) obj;
			Map<String, Object> map = new HashMap<String, Object>();
			Iterator it = jsonObject.keys();
			while (it.hasNext()) 
				String key = (String) it.next();
				Object value = jsonObject.get(key);
				map.put((String) key, value);
			
			list.add(map);
		
		return list;
	
	
	// 返回非实体类型(Map<String,Object>)的List
	public static List<Map<String, Object>> toList(JSONArray jsonArray) 
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (Object obj : jsonArray) 
			JSONObject jsonObject = (JSONObject) obj;
			Map<String, Object> map = new HashMap<String, Object>();
			Iterator it = jsonObject.keys();
			while (it.hasNext()) 
				String key = (String) it.next();
				Object value = jsonObject.get(key);
				map.put((String) key, value);
			
			list.add(map);
		
		return list;
	

	/***
	 * 将JSON对象数组转换为传入类型的List
	 * 
	 * @param <T>
	 * @param jsonArray
	 * @param objectClass
	 * @return
	 */
	public static <T> List<T> toList(JSONArray jsonArray, Class<T> objectClass) 
		return JSONArray.toList(jsonArray, objectClass);
	

	/***
	 * 将对象转换为传入类型的List
	 * 
	 * @param <T>
	 * @param jsonArray
	 * @param objectClass
	 * @return
	 */
	public static <T> List<T> toList(Object object, Class<T> objectClass) 
		JSONArray jsonArray = JSONArray.fromObject(object);

		return JSONArray.toList(jsonArray, objectClass);
	

	/***
	 * 将JSON对象转换为传入类型的对象
	 * 
	 * @param <T>
	 * @param jsonObject
	 * @param beanClass
	 * @return
	 */
	public static <T> T toBean(JSONObject jsonObject, Class<T> beanClass) 
		return (T) JSONObject.toBean(jsonObject, beanClass);
	

	/***
	 * 将将对象转换为传入类型的对象
	 * 
	 * @param <T>
	 * @param object
	 * @param beanClass
	 * @return
	 */
	public static <T> T toBean(Object object, Class<T> beanClass) 
		JSONObject jsonObject = JSONObject.fromObject(object);

		return (T) JSONObject.toBean(jsonObject, beanClass);
	

	/***
	 * 将JSON文本反序列化为主从关系的实体
	 * 
	 * @param <T>
	 *            泛型T 代表主实体类型
	 * @param <D>
	 *            泛型D 代表从实体类型
	 * @param jsonString
	 *            JSON文本
	 * @param mainClass
	 *            主实体类型
	 * @param detailName
	 *            从实体类在主实体类中的属性名称
	 * @param detailClass
	 *            从实体类型
	 * @return
	 */
	public static <T, D> T toBean(String jsonString, Class<T> mainClass,
			String detailName, Class<D> detailClass) 
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		JSONArray jsonArray = (JSONArray) jsonObject.get(detailName);

		T mainEntity = JSONHelper.toBean(jsonObject, mainClass);
		List<D> detailList = JSONHelper.toList(jsonArray, detailClass);

		try 
			BeanUtils.setProperty(mainEntity, detailName, detailList);
		 catch (Exception ex) 
			throw new RuntimeException("主从关系JSON反序列化实体失败!");
		

		return mainEntity;
	

	/***
	 * 将JSON文本反序列化为主从关系的实体
	 * 
	 * @param <T>泛型T 代表主实体类型
	 * @param <D1>泛型D1 代表从实体类型
	 * @param <D2>泛型D2 代表从实体类型
	 * @param jsonString
	 *            JSON文本
	 * @param mainClass
	 *            主实体类型
	 * @param detailName1
	 *            从实体类在主实体类中的属性
	 * @param detailClass1
	 *            从实体类型
	 * @param detailName2
	 *            从实体类在主实体类中的属性
	 * @param detailClass2
	 *            从实体类型
	 * @return
	 */
	public static <T, D1, D2> T toBean(String jsonString, Class<T> mainClass,
			String detailName1, Class<D1> detailClass1, String detailName2,
			Class<D2> detailClass2) 
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		JSONArray jsonArray1 = (JSONArray) jsonObject.get(detailName1);
		JSONArray jsonArray2 = (JSONArray) jsonObject.get(detailName2);

		T mainEntity = JSONHelper.toBean(jsonObject, mainClass);
		List<D1> detailList1 = JSONHelper.toList(jsonArray1, detailClass1);
		List<D2> detailList2 = JSONHelper.toList(jsonArray2, detailClass2);

		try 
			BeanUtils.setProperty(mainEntity, detailName1, detailList1);
			BeanUtils.setProperty(mainEntity, detailName2, detailList2);
		 catch (Exception ex) 
			throw new RuntimeException("主从关系JSON反序列化实体失败!");
		

		return mainEntity;
	

	/***
	 * 将JSON文本反序列化为主从关系的实体
	 * 
	 * @param <T>泛型T 代表主实体类型
	 * @param <D1>泛型D1 代表从实体类型
	 * @param <D2>泛型D2 代表从实体类型
	 * @param jsonString
	 *            JSON文本
	 * @param mainClass
	 *            主实体类型
	 * @param detailName1
	 *            从实体类在主实体类中的属性
	 * @param detailClass1
	 *            从实体类型
	 * @param detailName2
	 *            从实体类在主实体类中的属性
	 * @param detailClass2
	 *            从实体类型
	 * @param detailName3
	 *            从实体类在主实体类中的属性
	 * @param detailClass3
	 *            从实体类型
	 * @return
	 */
	public static <T, D1, D2, D3> T toBean(String jsonString,
			Class<T> mainClass, String detailName1, Class<D1> detailClass1,
			String detailName2, Class<D2> detailClass2, String detailName3,
			Class<D3> detailClass3) 
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		JSONArray jsonArray1 = (JSONArray) jsonObject.get(detailName1);
		JSONArray jsonArray2 = (JSONArray) jsonObject.get(detailName2);
		JSONArray jsonArray3 = (JSONArray) jsonObject.get(detailName3);

		T mainEntity = JSONHelper.toBean(jsonObject, mainClass);
		List<D1> detailList1 = JSONHelper.toList(jsonArray1, detailClass1);
		List<D2> detailList2 = JSONHelper.toList(jsonArray2, detailClass2);
		List<D3> detailList3 = JSONHelper.toList(jsonArray3, detailClass3);

		try 
			BeanUtils.setProperty(mainEntity, detailName1, detailList1);
			BeanUtils.setProperty(mainEntity, detailName2, detailList2);
			BeanUtils.setProperty(mainEntity, detailName3, detailList3);
		 catch (Exception ex) 
			throw new RuntimeException("主从关系JSON反序列化实体失败!");
		

		return mainEntity;
	

	/***
	 * 将JSON文本反序列化为主从关系的实体
	 * 
	 * @param <T>
	 *            主实体类型
	 * @param jsonString
	 *            JSON文本
	 * @param mainClass
	 *            主实体类型
	 * @param detailClass
	 *            存放了多个从实体在主实体中属性名称和类型
	 * @return
	 */
	public static <T> T toBean(String jsonString, Class<T> mainClass,
			HashMap<String, Class> detailClass) 
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		T mainEntity = JSONHelper.toBean(jsonObject, mainClass);
		for (Object key : detailClass.keySet()) 
			try 
				Class value = (Class) detailClass.get(key);
				BeanUtils.setProperty(mainEntity, key.toString(), value);
			 catch (Exception ex) 
				throw new RuntimeException("主从关系JSON反序列化实体失败!");
			
		
		return mainEntity;
	
	
	public static String listtojson(String[] fields, int total, List list) throws Exception 
		Object[] values = new Object[fields.length];
		String jsonTemp = "\\"total\\":" + total + ",\\"rows\\":[";
		for (int j = 0; j < list.size(); j++) 
			jsonTemp = jsonTemp + "\\"state\\":\\"closed\\",";
			for (int i = 0; i < fields.length; i++) 
				String fieldName = fields[i].toString();
				values[i] = org.jeecgframework.tag.core.easyui.TagUtil.fieldNametoValues(fieldName, list.get(j));
				jsonTemp = jsonTemp + "\\"" + fieldName + "\\"" + ":\\"" + values[i] + "\\"";
				if (i != fields.length - 1) 
					jsonTemp = jsonTemp + ",";
				
			
			if (j != list.size() - 1) 
				jsonTemp = jsonTemp + ",";
			 else 
				jsonTemp = jsonTemp + "";
			
		
		jsonTemp = jsonTemp + "]";
		return jsonTemp;
	



以上是关于JSON和JAVA相互转换的主要内容,如果未能解决你的问题,请参考以下文章

JSON和JAVA相互转换

Jackson 对象和JSON的相互转换

工具类|java实现Json和XML相互转换

java JSON 和 Object 相互转换

xml和JSON格式相互转换的Java实现

使用JSONObject将实体类,String类型和JSON类型相互转换(java)