?*?Copyright?(c)?linkwise?2007-2009?corporation.??
?*?All?rights?reserved
?*/
package?com.linghui.common.util;
import?java.util.ArrayList;
import?java.util.Date;
import?java.util.HashMap;
import?java.util.Iterator;
import?java.util.List;
import?java.util.Map;
import?net.sf.json.JSONArray;
import?net.sf.json.JSONObject;
import?net.sf.json.JsonConfig;
import?net.sf.json.util.CycleDetectionStrategy;
import?com.linghui.common.util.DateUtil;
import?com.linghui.common.util.jsonutil.DateJsonValueProcessor;
/**
?*?@author?</br>?<a?href="mailto:fx19800215@163.com">?robert.feng</a>
?*
?*/
public?class?JsonUtil?{
????/**
?????*?从一个JSON?对象字符格式中得到一个java对象
?????*?@param?jsonString
?????*?@param?pojoCalss
?????*?@return
?????*/
????public?static?Object?getObject4JsonString(String?jsonString,Class?pojoCalss){
????????Object?pojo;
????????JSONObject?jsonObject?=?JSONObject.fromObject(?jsonString?);??
????????pojo?=?JSONObject.toBean(jsonObject,pojoCalss);
????????return?pojo;
????}
????
????
????
????/**
?????*?从json?HASH表达式中获取一个map,改map支持嵌套功能
?????*?@param?jsonString
?????*?@return
?????*/
????public?static?Map?getMap4Json(String?jsonString){
????????JSONObject?jsonObject?=?JSONObject.fromObject(?jsonString?);??
????????Iterator??keyIter?=?jsonObject.keys();
????????String?key;
????????Object?value;
????????Map?valueMap?=?new?HashMap();
????????while(?keyIter.hasNext())
????????{
????????????key?=?(String)keyIter.next();
????????????value?=?jsonObject.get(key);
????????????valueMap.put(key,?value);
????????}
????????
????????return?valueMap;
????}
????
????
????/**
?????*?从json数组中得到相应java数组
?????*?@param?jsonString
?????*?@return
?????*/
????public?static?Object[]?getObjectArray4Json(String?jsonString){
????????JSONArray?jsonArray?=?JSONArray.fromObject(jsonString);
????????return?jsonArray.toArray();
????????
????}
????
????
????/**
?????*?从json对象集合表达式中得到一个java对象列表
?????*?@param?jsonString
?????*?@param?pojoClass
?????*?@return
?????*/
????public?static?List?getList4Json(String?jsonString,?Class?pojoClass){
????????
????????JSONArray?jsonArray?=?JSONArray.fromObject(jsonString);
????????JSONObject?jsonObject;
????????Object?pojoValue;
????????
????????List?list?=?new?ArrayList();
????????for?(?int?i?=?0?;?i<jsonArray.size();?i++){
????????????
????????????jsonObject?=?jsonArray.getJSONObject(i);
????????????pojoValue?=?JSONObject.toBean(jsonObject,pojoClass);
????????????list.add(pojoValue);
????????????
????????}
????????return?list;
????}
????
????/**
?????*?从json数组中解析出java字符串数组
?????*?@param?jsonString
?????*?@return
?????*/
????public?static?String[]?getStringArray4Json(String?jsonString){
????????
????????JSONArray?jsonArray?=?JSONArray.fromObject(jsonString);
????????String[]?stringArray?=?new?String[jsonArray.size()];
????????for(?int?i?=?0?;?i<jsonArray.size()?;?i++?){
????????????stringArray[i]?=?jsonArray.getString(i);
????????????
????????}
????????
????????return?stringArray;
????}
????
????/**
?????*?从json数组中解析出javaLong型对象数组
?????*?@param?jsonString
?????*?@return
?????*/
????public?static?Long[]?getLongArray4Json(String?jsonString){
????????
????????JSONArray?jsonArray?=?JSONArray.fromObject(jsonString);
????????Long[]?longArray?=?new?Long[jsonArray.size()];
????????for(?int?i?=?0?;?i<jsonArray.size()?;?i++?){
????????????longArray[i]?=?jsonArray.getLong(i);
????????????
????????}
????????return?longArray;
????}
????
????/**
?????*?从json数组中解析出java?Integer型对象数组
?????*?@param?jsonString
?????*?@return
?????*/
????public?static?Integer[]?getIntegerArray4Json(String?jsonString){
????????
????????JSONArray?jsonArray?=?JSONArray.fromObject(jsonString);
????????Integer[]?integerArray?=?new?Integer[jsonArray.size()];
????????for(?int?i?=?0?;?i<jsonArray.size()?;?i++?){
????????????integerArray[i]?=?jsonArray.getInt(i);
????????????
????????}
????????return?integerArray;
????}
????
????/**
?????*?从json数组中解析出java?Date?型对象数组,使用本方法必须保证
?????*?@param?jsonString
?????*?@return
?????*/
????public?static?Date[]?getDateArray4Json(String?jsonString,String?DataFormat){
????????
????????JSONArray?jsonArray?=?JSONArray.fromObject(jsonString);
????????Date[]?dateArray?=?new?Date[jsonArray.size()];
????????String?dateString;
????????Date?date;
????????
????????for(?int?i?=?0?;?i<jsonArray.size()?;?i++?){
????????????dateString?=?jsonArray.getString(i);
????????????date?=?DateUtil.stringToDate(dateString,?DataFormat);
????????????dateArray[i]?=?date;
????????????
????????}
????????return?dateArray;
????}
????
????/**
?????*?从json数组中解析出java?Integer型对象数组
?????*?@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对象转换成json字符串
?????*?@param?javaObj
?????*?@return
?????*/
????public?static?String?getJsonString4JavaPOJO(Object?javaObj){
????????
????????JSONObject?json;
????????json?=?JSONObject.fromObject(javaObj);
????????return?json.toString();
????????
????}
????
????
????
????
????/**
?????*?将java对象转换成json字符串,并设定日期格式
?????*?@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?自动生成方法存根
????}
????
????/**
?????*?JSON?时间解析器具
?????*?@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;???
????????}??
}
?*?linkwise
?*/
package?com.linghui.common.util.jsonutil;
import?java.text.DateFormat;
import?java.text.SimpleDateFormat;
import?java.util.Date;
import?net.sf.json.JsonConfig;
import?net.sf.json.processors.JsonValueProcessor;
/**
?*??@author?</br>?<a?href="mailto:fx19800215@163.com">?robert.feng</a>
?*
?*/
public?class?DateJsonValueProcessor?implements?JsonValueProcessor?{
????
<img
详细解决方案
java处置JSON格式数据的通用类-分享
热度:384 发布时间:2012-12-22 12:05:07.0
/**
/**
相关解决方案
- java 乱码 汉字是 ? 如何转换成汉字
- (Struts2+JSON+Ajax) XMLHttpRequest ==500如何解决
- java web 登录次数限制,该如何解决
- json 解析有关问题
- java 工商银行网银支付 B2B的 都亟需什么,需要注意什么
- Java Applet程序从JDK6升级到7时遇到的有关问题
- java web 视频相干
- 请教一个关于链接后缀的有关问题(java web 应用)
- java 系统单点登录解决方案
- java.lang.NumberFormatException: For input string: "id"该如何处理
- java.lang.NoSuchMethodException: setId([Ljava.lang.String;)解决方法
- java 获取客户端IP解决办法
- JAVA 后台怎么获得前台页面FCKedit编辑器中的内容以及内容的样式
- java 正则化匹配有关问题
- java web 受阻
- java.lang.IllegalStateException: No output folder,该怎么解决
- struts2-json中诠注@JSON(deserialize=false)是什么用?不是阻止JSON反序列化成JAVA对象吗?求解
- java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.makeAccess,该如何解决
- java 乱码有关问题 急求大神
- java EE错误如何解决阿,都是过了,没有能解决的!
- java 工程打包有关问题 多谢各位大神!多谢
- JAVA WEB导航条,该怎么处理
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password,该如何处理
- java 中Node 有关问题
- 关于 java 引述传递和值传递,你的知否
- 吐了,java Timer 终止不了。
- Java Web 学习中有关问题,请高手指教
- java web中的url地址小疑点。
- java web开发解决办法
- java.lang.NullPointerException解决方法