日期:2014-05-16  浏览次数:20348 次

Java操作Json的工具类

package com.baiyyy.polabs.util.json;????????
import java.text.ParseException;
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;
???????
/**?????
* 处理json的工具类,负责json数据转换成java对象和java对象转换成json?????
*??????
* @author yongtree?????
* @date 2008-11-22 上午10:47:13?????
* @version 1.0?????
*/???????
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);????????
???????