Java操作JSON工具整理

來源:互聯網
上載者:User

一、配置 開發運行環境

Json必需的包

commons-httpclient-3.1.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
json-lib-2.2.3-jdk13.jar
ezmorph-1.0.6.jar
commons-collections-3.2.1.jar

xom-1.0b3.jar

commons-beanutils.jar

以上包可以從

http://commons.apache.org/index.html

http://json-lib.sourceforge.net/

http://ezmorph.sourceforge.net/

http://morph.sourceforge.net/

http://www.docjar.com/

  1. package com.baiyyy.polabs.util.json;  
  2.   
  3. import java.text.ParseException;  
  4. import java.util.ArrayList;  
  5. import java.util.Date;  
  6. import java.util.HashMap;  
  7. import java.util.Iterator;  
  8. import java.util.List;  
  9. import java.util.Map;  
  10.   
  11. import com.baiyyy.polabs.util.DateUtil;  
  12.   
  13. import net.sf.json.JSONArray;  
  14. import net.sf.json.JSONObject;  
  15. import net.sf.json.JsonConfig;  
  16. import net.sf.json.util.CycleDetectionStrategy;  
  17.   
  18. /** 
  19.  * 處 理json的工具類,負責json資料轉換成java對象和java對象轉換成json 
  20. public class JsonUtil {  
  21.   
  22.     /** 
  23.      * 從 一個JSON 對象字元格式設定中得到一個java對象 
  24.      *  
  25.      * @param jsonString 
  26.      * @param pojoCalss 
  27.      * @return 
  28.      */  
  29.     public static Object getObject4JsonString(String jsonString, Class pojoCalss) {  
  30.         Object pojo;  
  31.         JSONObject jsonObject = JSONObject.fromObject(jsonString);  
  32.         pojo = JSONObject.toBean(jsonObject, pojoCalss);  
  33.         return pojo;  
  34.     }  
  35.   
  36.   
  37.     /** 
  38.      * 從 json HASH運算式中擷取一個map,改map支援嵌套功能 
  39.      *  
  40.      * @param jsonString 
  41.      * @return 
  42.      */  
  43.     public static Map getMap4Json(String jsonString) {  
  44.         JSONObject jsonObject = JSONObject.fromObject(jsonString);  
  45.         Iterator keyIter = jsonObject.keys();  
  46.         String key;  
  47.         Object value;  
  48.         Map valueMap = new HashMap();  
  49.   
  50.         while (keyIter.hasNext()) {  
  51.             key = (String) keyIter.next();  
  52.             value = jsonObject.get(key);  
  53.             valueMap.put(key, value);  
  54.         }  
  55.   
  56.         return valueMap;  
  57.     }  
  58.   
  59.   
  60.     /** 
  61.      * 從 json數組中得到相應java數組 
  62.      *  
  63.      * @param jsonString 
  64.      * @return 
  65.      */  
  66.     public static Object[] getObjectArray4Json(String jsonString) {  
  67.         JSONArray jsonArray = JSONArray.fromObject(jsonString);  
  68.         return jsonArray.toArray();  
  69.   
  70.     }  
  71.   
  72.   
  73.     /** 
  74.      * 從 json對象集合運算式中得到一個java對象列表 
  75.      *  
  76.      * @param jsonString 
  77.      * @param pojoClass 
  78.      * @return 
  79.      */  
  80.     public static List getList4Json(String jsonString, Class pojoClass) {  
  81.   
  82.         JSONArray jsonArray = JSONArray.fromObject(jsonString);  
  83.         JSONObject jsonObject;  
  84.         Object pojoValue;  
  85.   
  86.         List list = new ArrayList();  
  87.         for (int i = 0; i < jsonArray.size(); i++) {  
  88.   
  89.             jsonObject = jsonArray.getJSONObject(i);  
  90.             pojoValue = JSONObject.toBean(jsonObject, pojoClass);  
  91.             list.add(pojoValue);  
  92.   
  93.         }  
  94.         return list;  
  95.   
  96.     }  
  97.   
  98.   
  99.     /** 
  100.      * 從 json數組中解析出java字串數組 
  101.      *  
  102.      * @param jsonString 
  103.      * @return 
  104.      */  
  105.     public static String[] getStringArray4Json(String jsonString) {  
  106.   
  107.         JSONArray jsonArray = JSONArray.fromObject(jsonString);  
  108.         String[] stringArray = new String[jsonArray.size()];  
  109.         for (int i = 0; i < jsonArray.size(); i++) {  
  110.             stringArray[i] = jsonArray.getString(i);  
  111.   
  112.         }  
  113.   
  114.         return stringArray;  
  115.     }  
  116.   
  117.   
  118.     /** 
  119.      * 從 json數組中解析出javaLong型對象數組 
  120.      *  
  121.      * @param jsonString 
  122.      * @return 
  123.      */  
  124.     public static Long[] getLongArray4Json(String jsonString) {  
  125.   
  126.         JSONArray jsonArray = JSONArray.fromObject(jsonString);  
  127.         Long[] longArray = new Long[jsonArray.size()];  
  128.         for (int i = 0; i < jsonArray.size(); i++) {  
  129.             longArray[i] = jsonArray.getLong(i);  
  130.   
  131.         }  
  132.         return longArray;  
  133.     }  
  134.   
  135.   
  136.     /** 
  137.      * 從 json數組中解析出java Integer型對象數組 
  138.      *  
  139.      * @param jsonString 
  140.      * @return 
  141.      */  
  142.     public static Integer[] getIntegerArray4Json(String jsonString) {  
  143.   
  144.         JSONArray jsonArray = JSONArray.fromObject(jsonString);  
  145.         Integer[] integerArray = new Integer[jsonArray.size()];  
  146.         for (int i = 0; i < jsonArray.size(); i++) {  
  147.             integerArray[i] = jsonArray.getInt(i);  
  148.   
  149.         }  
  150.         return integerArray;  
  151.     }  
  152.   
  153.     /** 
  154.      * 從 json數組中解析出java Date 型對象數組,使用本方法必須保證 
  155.      *  
  156.      * @param jsonString 
  157.      * @return 
  158.      * @throws ParseException 
  159.      */  
  160.     public static Date[] getDateArray4Json(String jsonString, String DataFormat)  
  161.             throws ParseException {  
  162.   
  163.         JSONArray jsonArray = JSONArray.fromObject(jsonString);  
  164.         Date[] dateArray = new Date[jsonArray.size()];  
  165.         String dateString;  
  166.         Date date;  
  167.   
  168.         for (int i = 0; i < jsonArray.size(); i++) {  
  169.             dateString = jsonArray.getString(i);  
  170.             date = DateUtil.parseDate(dateString, DataFormat);  
  171.             dateArray[i] = date;  
  172.   
  173.         }  
  174.         return dateArray;  
  175.     }  
  176.   
  177.   
  178.     /** 
  179.      * 從 json數組中解析出java Integer型對象數組 
  180.      *  
  181.      * @param jsonString 
  182.      * @return 
  183.      */  
  184.     public static Double[] getDoubleArray4Json(String jsonString) {  
  185.   
  186.         JSONArray jsonArray = JSONArray.fromObject(jsonString);  
  187.         Double[] doubleArray = new Double[jsonArray.size()];  
  188.         for (int i = 0; i < jsonArray.size(); i++) {  
  189.             doubleArray[i] = jsonArray.getDouble(i);  
  190.   
  191.         }  
  192.         return doubleArray;  
  193.     }  
  194.   
  195.   
  196.     /** 
  197.      * 將 java對象轉換成json字串 
  198.      *  
  199.      * @param javaObj 
  200.      * @return 
  201.      */  
  202.     public static String getJsonString4JavaPOJO(Object javaObj) {  
  203.   
  204.         JSONObject json;  
  205.         json = JSONObject.fromObject(javaObj);  
  206.         return json.toString();  
  207.   
  208.     }  
  209. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.