開發:隨筆記錄之 Json字串 與 List、HashMap物件的轉換

執筆記憶的空白發表於2014-05-04
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;


public class JsonParser {


/**
* json字串 轉成 map

* @param jsonStr
* @return
* @throws Exception
*/

public static HashMap<String, JsonValue> parse(String jsonStr) {
if (jsonStr == null || "".equals(jsonStr)) { return null; }
HashMap<String, JsonValue> retMap = null;
try {
retMap = new HashMap<String, JsonValue>();
JSONObject json = JSONObject.fromObject(jsonStr);
Map<String, Object> tmpMap = (Map<String, Object>) JSONObject
.toBean(json, Map.class);
for (Map.Entry<String, Object> entry : tmpMap.entrySet()) {
JsonValue tmp = parseRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retMap;
}


/**
* json字串 轉成 List

* @param jsonStr
* @return
* @throws Exception
*/

public static List<HashMap<String, JsonValue>> parseList(String jsonStr) {
if (jsonStr == null || "".equals(jsonStr)) { return null; }


List<HashMap<String, JsonValue>> retList = new ArrayList<HashMap<String, JsonValue>>();


JSONArray data = JSONArray.fromObject(jsonStr);
for (int i = 0; i < data.size(); i++) {
HashMap<String, JsonValue> retMap = new HashMap<String, JsonValue>();


JSONObject json = (JSONObject) data.get(i);
Map<String, Object> tmpMap = (Map<String, Object>) JSONObject
.toBean(json, Map.class);


for (Map.Entry<String, Object> entry : tmpMap.entrySet()) {
JsonValue tmp = parseRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
retList.add(retMap);
}
return retList;
}


/**
* HashMap<String, JsonValue> map 轉成 json字串

* @param jsonStr
* @return
* @throws Exception
*/

public static String parse(HashMap<String, JsonValue> map) {
HashMap<String, Object> retMap = new HashMap<String, Object>();
for (Map.Entry<String, JsonValue> entry : map.entrySet()) {
Object tmp = parseJsonValueRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
JsonConfig jc = new JsonConfig();
return JSONObject.fromObject(retMap, jc).toString();
}


/**
* List<HashMap<String, JsonValue>> list 轉成 json字串

* @param jsonStr
* @return
* @throws Exception
*/

public static String parse(List<HashMap<String, JsonValue>> list) {
List<HashMap<String, Object>> tmpList = new ArrayList<HashMap<String, Object>>();


for (HashMap<String, JsonValue> map : list) {
HashMap<String, Object> retMap = new HashMap<String, Object>();
for (Map.Entry<String, JsonValue> entry : map.entrySet()) {
Object tmp = parseJsonValueRec(entry.getValue(), 0);
retMap.put(entry.getKey(), tmp);
}
tmpList.add(retMap);
}
JSONArray json = new JSONArray();
json.addAll(tmpList);
return json.toString();
}


/**
* 構建json

* @param map
* @return
*/

public static String parse(Map map) {
JsonConfig jc = new JsonConfig();
return JSONObject.fromObject(map, jc).toString();
}

      對了,json的版本我用的是json-lib-2.4-jdk15.jar

相關文章