1. 程式人生 > >JSON 解析工具的封裝(FastJSON-->Java)

JSON 解析工具的封裝(FastJSON-->Java)

str ras bool return pass pre mail lar 格式化json

1.添加依賴包

 <dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.1.41</version>
</dependency>

2.代碼

package com.yys.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer; import com.alibaba.fastjson.serializer.SerializeConfig; import com.alibaba.fastjson.serializer.SerializerFeature; import com.sun.tools.doclets.formats.html.SourceToHTMLConverter;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * User: 楊永生 * Date: 9:24 2017/9/6 * Email: [email protected] */ public class FastJsonUtils { private final static Logger logger = LoggerFactory.getLogger(FastJsonUtils.class
); private static SerializeConfig config; static { config = new SerializeConfig(); config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期輸出格式 config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期輸出格式 } private static final SerializerFeature[] features = { SerializerFeature.DisableCircularReferenceDetect,//打開循環引用檢測,JSONField(serialize = false)不循環 SerializerFeature.WriteDateUseDateFormat,//默認使用系統默認 格式日期格式化 SerializerFeature.WriteMapNullValue, //輸出空置字段 SerializerFeature.WriteNullListAsEmpty,//list字段如果為null,輸出為[],而不是null SerializerFeature.WriteNullNumberAsZero,// 數值字段如果為null,輸出為0,而不是null SerializerFeature.WriteNullBooleanAsFalse,//Boolean字段如果為null,輸出為false,而不是null SerializerFeature.WriteNullStringAsEmpty//字符類型字段如果為null,輸出為"",而不是null }; /** * 返回Json字符串裏面包含的一個對象 * * @param jsonStr :{"city":"china","map1":[{"age":"28","name":"yys"}]} * @param list_str :map1 * @param clazz :Map.class,或者其他對象 * @param <T> :Map * @return :List<Map> */ public static final <T> List<T> json2childList(String jsonStr, String list_str, Class<T> clazz) { JSONObject jsonobj = JSON.parseObject(jsonStr); if (jsonobj == null) { return null; } Object obj = jsonobj.get(list_str); if (obj == null) { return null; } 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; } /** * 返回Json字符串裏面包含的一個對象 * * @param jsonStr :{"department":{"id":"1","name":"生產部"},"password":"admin","username":"admin"} * @param obj_str :department * @param clazz :Map.class,或者其他對象 * @param <T> :Map * @return */ public static final <T> T json2childObj(String jsonStr, String obj_str, Class<T> clazz) { JSONObject jsonobj = JSON.parseObject(jsonStr); 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; } /** * json 轉換成對象 * * @param jsonStr * @param clazz * @param <T> * @return */ public static final <T> T json2obj(String jsonStr, Class<T> clazz) { T t = null; try { t = JSON.parseObject(jsonStr, clazz); } catch (Exception e) { logger.error("json字符串轉換失敗!" + jsonStr, e); } return t; } /** * 對象轉換成json字符串(帶有格式化輸出) * * @param object 要轉換的對象 * @param prettyFormat 是否格式化json字符串,輸出帶有換行和縮進的字符串 * @return 返回一個json 字符串數組 */ public static final String obj2json(Object object, boolean prettyFormat) { return JSON.toJSONString(object, prettyFormat); } /** * 對象轉換成json字符串 * * @param object 要轉換的對象 * @return 返回一個json 字符串數組 */ public static final String obj2jsonByFeatures(Object object) { return JSON.toJSONString(object,config,features); } /** * 對象轉換成json字符串 * * @param object 要轉換的對象 * @return 返回一個json 字符串數組 */ public static final String obj2json(Object object) { return JSON.toJSONString(object,config); } /** * json 字符串轉換成原始的Object對象 * * @param jsonStr * @return */ public static final Object json2obj(String jsonStr) { return JSON.parse(jsonStr); } /** * json字符串轉換成list * * @param jsonStr Json字符串 * @param clazz 要轉換的class * @param <T>返回的泛型類型 * @return 返回的<T>泛型類型 */ public static <T> List<T> json2List(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; } /** * json字符串轉換成Map * * @param jsonStr Json字符串 * @return Map */ 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; } /** * json 轉換成list<map> * * @param jsonString * @return */ public static List<Map<String, Object>> json2ListkeyMap(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; } }

JSON 解析工具的封裝(FastJSON-->Java)