javascript
一个java处理JSON格式数据的通用类(三)
/** *//**
????? * 從json數(shù)組中解析出java Integer型對(duì)象數(shù)組
????? * @param jsonString
????? * @return
?????*/
????public static Double[] getDoubleArray4Json(String jsonString)...{
????????
???????? JSONArray jsonArray = JSONArray.fromObject(jsonString);
???????? Double[] doubleArray = new Double[jsonArray.size()];
????????for( int i = 0 ; i<jsonArray.size() ; i++ )...{
???????????? doubleArray[i] = jsonArray.getDouble(i);
????????????
???????? }
????????return doubleArray;
???? }
????
????
????/** *//**
????? * 將java對(duì)象轉(zhuǎn)換成json字符串
????? * @param javaObj
????? * @return
?????*/
????public static String getJsonString4JavaPOJO(Object javaObj)...{
????????
???????? JSONObject json;
???????? json = JSONObject.fromObject(javaObj);
????????return json.toString();
????????
???? }
????
????
????
????
????/** *//**
????? * 將java對(duì)象轉(zhuǎn)換成json字符串,并設(shè)定日期格式
????? * @param javaObj
????? * @param dataFormat
????? * @return
?????*/
????public static String getJsonString4JavaPOJO(Object javaObj , String dataFormat)...{
????????
???????? JSONObject json;
???????? JsonConfig jsonConfig = configJson(dataFormat);
???????? json = JSONObject.fromObject(javaObj,jsonConfig);
????????return json.toString();
????????
????????
???? }
????
????
????
????/** *//**
????? * @param args
?????*/
????public static void main(String[] args) ...{
????????// TODO 自動(dòng)生成方法存根
???? }
????
????/** *//**
????? * JSON 時(shí)間解析器具
????? * @param datePattern
????? * @return
?????*/
????public static JsonConfig configJson(String datePattern) ...{???
???????????? JsonConfig jsonConfig = new JsonConfig();???
???????????? jsonConfig.setExcludes(new String[]...{""});???
???????????? jsonConfig.setIgnoreDefaultExcludes(false);???
???????????? jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);???
???????????? jsonConfig.registerJsonValueProcessor(Date.class,???
????????????????new DateJsonValueProcessor(datePattern));???
??????????
????????????return jsonConfig;???
???????? }??
????
????/** *//**
????? *?
????? * @param excludes
????? * @param datePattern
????? * @return
?????*/
????public static JsonConfig configJson(String[] excludes,???
???????????? String datePattern) ...{???
???????????? JsonConfig jsonConfig = new JsonConfig();???
???????????? jsonConfig.setExcludes(excludes);???
???????????? jsonConfig.setIgnoreDefaultExcludes(false);???
???????????? jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);???
???????????? jsonConfig.registerJsonValueProcessor(Date.class,???
????????????????new DateJsonValueProcessor(datePattern));???
??????????
????????????return jsonConfig;???
???????? }??
}
轉(zhuǎn)載于:https://www.cnblogs.com/Struts-pring/archive/2013/05/30/3108250.html
總結(jié)
以上是生活随笔為你收集整理的一个java处理JSON格式数据的通用类(三)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GridView 移除模板列
- 下一篇: [转]可伸缩系统的架构经验