1. 程式人生 > >處理json的工具類({本類為處理json的工具類})

處理json的工具類({本類為處理json的工具類})

<jackson.version>2.2.3</jackson.version>

<!-- json -->
<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.3</version>
    <classifier>jdk15</classifier>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.1.41</version>
</dependency>
<!-- jackson json -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.module</groupId>
    <artifactId>jackson-module-jaxb-annotations</artifactId>
    <version>${jackson.version}</version>
</dependency>
package com.dsj.gdbd.utils.jsonUtil;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import
java.util.Set; import org.apache.commons.collections.map.ListOrderedMap; import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.apache.log4j.Logger; import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** * * 處理json的工具類. <br> * 本類為處理json的工具類 * *
@author slj */ public class JsonTools { private static Logger log = Logger.getLogger(JsonTools.class); /** * * json轉換list. <br> * 詳細說明 * * @param jsonStr * json字串 * @return * @return List<Map<String,Object>> list * @throws @author * slj * @date 2013年12月24日 下午1:08:03 */ public static List<Map<String, Object>> parseJSON2List(String jsonStr) { JSONArray jsonArr = JSONArray.fromObject(jsonStr); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Iterator<JSONObject> it = jsonArr.iterator(); while (it.hasNext()) { JSONObject json2 = it.next(); list.add(parseJSON2Map(json2.toString())); } return list; } /** * * json轉換map. <br> * 詳細說明 * * @param jsonStr * json字串 * @return * @return Map<String,Object> 集合 * @throws @author * slj */ public static Map<String, Object> parseJSON2Map(String jsonStr) { ListOrderedMap map = new ListOrderedMap(); // 最外層解析 JSONObject json = JSONObject.fromObject(jsonStr); for (Object k : json.keySet()) { Object v = json.get(k); // 如果內層還是陣列的話,繼續解析 if (v instanceof JSONArray) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); Iterator<JSONObject> it = ((JSONArray) v).iterator(); while (it.hasNext()) { JSONObject json2 = it.next(); list.add(parseJSON2Map(json2.toString())); } map.put(k.toString(), list); } else { map.put(k.toString(), v); } } return map; } /** * * 通過HTTP獲取JSON資料. <br> * 通過HTTP獲取JSON資料返回list * * @param url * 連結 * @return * @return List<Map<String,Object>> list * @throws @author * slj */ public static List<Map<String, Object>> getListByUrl(String url) { try { // 通過HTTP獲取JSON資料 InputStream in = new URL(url).openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } return parseJSON2List(sb.toString()); } catch (Exception e) { log.error("Exception", e); } return null; } /** * * 通過HTTP獲取JSON資料. <br> * 通過HTTP獲取JSON資料返回map * * @param url * 連結 * @return * @return Map<String,Object> 集合 * @throws @author * slj */ public static Map<String, Object> getMapByUrl(String url) { try { // 通過HTTP獲取JSON資料 InputStream in = new URL(url).openStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder sb = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } return parseJSON2Map(sb.toString()); } catch (Exception e) { log.error("Exception", e); } return null; } /** * * map轉換json. <br> * 詳細說明 * * @param map * 集合 * @return * @return String json字串 * @throws @author * slj */ public static String mapToJson(Map<String, Object> map) { Set<String> keys = map.keySet(); String key=""; StringBuffer jsonBuffer = new StringBuffer(); jsonBuffer.append("{"); for (Iterator<String> it = keys.iterator(); it.hasNext();) { key = (String) it.next(); String value = ObjectUtils.toString(map.get(key), ""); jsonBuffer.append(key + ":" + "\"" + value + "\""); if (it.hasNext()) { jsonBuffer.append(","); } } jsonBuffer.append("}"); return jsonBuffer.toString(); } /** * * map轉換json. <br> * 詳細說明 * * @param map * 集合 * @return * @return String json字串 * @throws @author * slj */ public static String mapToKvJson(Map<String, Object> map) { Set<String> keys = map.keySet(); JSONArray jsonArray=new JSONArray(); String key = ""; for (Iterator<String> it = keys.iterator(); it.hasNext();) { JSONObject object = new JSONObject(); key = (String) it.next(); String value = ObjectUtils.toString(map.get(key), ""); object.put("key", key); object.put("value", value); jsonArray.add(object); } return jsonArray.toString(); } /** * key value轉map [{"name":"sEcho","value":1},{"name":"iColumns","value":3}]轉map * @param jsonStr * @return */ public static Map<String, Object> parsePageMap(String jsonStr){ Map<String, Object> map = new HashMap<String, Object>(); //最外層解析 if(StringUtils.isNotBlank(jsonStr)){ jsonStr=jsonStr.replace("&quot;", "\""); } JSONArray jsonArray = JSONArray.fromObject(jsonStr); for (int i = 0; i < jsonArray.size(); i++) { JSONObject json = (JSONObject) jsonArray.get(i); map.put(json.getString("name"), json.getString("value")); } return map; } // test public static void main(String[] args) { Map<String, Object> map=new HashMap<String, Object>(); map.put("2", "1"); map.put("3", "1"); System.out.println(mapToKvJson(map)); } }