zoukankan      html  css  js  c++  java
  • java对象转化为json字符串并传到前台

    Java代码  收藏代码
    1. package cc.util;  
    2.   
    3. import java.util.ArrayList;  
    4.   
    5. import java.util.Date;  
    6.   
    7. import java.util.HashMap;  
    8.   
    9. import java.util.Iterator;  
    10.   
    11. import java.util.List;  
    12.   
    13. import java.util.Map;  
    14.   
    15. import   ccmodel.DateJsonValueProcessor;  
    16.   
    17.   
    18.   
    19.    
    20.   
    21. import net.sf.json.JSONArray;  
    22.   
    23. import net.sf.json.JSONObject;  
    24.   
    25. import net.sf.json.JsonConfig;  
    26.   
    27. import net.sf.json.util.CycleDetectionStrategy;  
    28.   
    29.    
    30. public class JsonUtil {  
    31.      /** 
    32.  
    33.      * 从一个JSON 对象字符格式中得到一个java对象 
    34.  
    35.      * @param jsonString 
    36.  
    37.      * @param pojoCalss 
    38.  
    39.      * @return 
    40.  
    41.      */  
    42.   
    43.      public static Object getObject4JsonString(String jsonString,Class pojoCalss){  
    44.   
    45.      Object pojo;  
    46.   
    47.      JSONObject jsonObject = JSONObject.fromObject( jsonString );   
    48.   
    49.      pojo = JSONObject.toBean(jsonObject,pojoCalss);  
    50.   
    51.      return pojo;  
    52.   
    53.      }   
    54.   
    55.        
    56.   
    57.      /** 
    58.  
    59.      * 从json HASH表达式中获取一个map,改map支持嵌套功能 
    60.  
    61.      * @param jsonString 
    62.  
    63.      * @return 
    64.  
    65.      */  
    66.   
    67.      public static Map getMap4Json(String jsonString){  
    68.   
    69.      JSONObject jsonObject = JSONObject.fromObject( jsonString );   
    70.   
    71.      Iterator keyIter = jsonObject.keys();  
    72.   
    73.      String key;  
    74.   
    75.      Object value;  
    76.   
    77.      Map valueMap = new HashMap();  
    78.   
    79.      while( keyIter.hasNext())  
    80.   
    81.      {  
    82.   
    83.      key = (String)keyIter.next();  
    84.   
    85.      value = jsonObject.get(key);  
    86.   
    87.      valueMap.put(key, value);  
    88.   
    89.      }   
    90.   
    91.      return valueMap;  
    92.   
    93.      }  
    94.   
    95.        
    96.   
    97.        
    98.   
    99.      /** 
    100.  
    101.      * 从json数组中得到相应java数组 
    102.  
    103.      * @param jsonString 
    104.  
    105.      * @return 
    106.  
    107.      */  
    108.   
    109.      public static Object[] getObjectArray4Json(String jsonString){  
    110.   
    111.      JSONArray jsonArray = JSONArray.fromObject(jsonString);  
    112.   
    113.      return jsonArray.toArray();   
    114.   
    115.      }  
    116.   
    117.        
    118.   
    119.        
    120.   
    121.      /** 
    122.  
    123.      * 从json对象集合表达式中得到一个java对象列表 
    124.  
    125.      * @param jsonString 
    126.  
    127.      * @param pojoClass 
    128.  
    129.      * @return 
    130.  
    131.      */  
    132.   
    133.      public static List getList4Json(String jsonString, Class pojoClass){   
    134.   
    135.      JSONArray jsonArray = JSONArray.fromObject(jsonString);  
    136.   
    137.      JSONObject jsonObject;  
    138.   
    139.      Object pojoValue;   
    140.   
    141.      List list = new ArrayList();  
    142.   
    143.      for ( int i = 0 ; i<jsonArray.size(); i++){   
    144.   
    145.      jsonObject = jsonArray.getJSONObject(i);  
    146.   
    147.      pojoValue = JSONObject.toBean(jsonObject,pojoClass);  
    148.   
    149.      list.add(pojoValue);  
    150.   
    151.       }  
    152.   
    153.      return list;  
    154.   
    155.      }  
    156.   
    157.        
    158.   
    159.      /** 
    160.  
    161.      * 从json数组中解析出java字符串数组 
    162.  
    163.      * @param jsonString 
    164.  
    165.      * @return 
    166.  
    167.      */  
    168.   
    169.      public static String[] getStringArray4Json(String jsonString){   
    170.   
    171.      JSONArray jsonArray = JSONArray.fromObject(jsonString);  
    172.   
    173.      String[] stringArray = new String[jsonArray.size()];  
    174.   
    175.      forint i = 0 ; i<jsonArray.size() ; i++ ){  
    176.   
    177.      stringArray[i] = jsonArray.getString(i);   
    178.   
    179.      }   
    180.   
    181.      return stringArray;  
    182.   
    183.      }  
    184.   
    185.        
    186.   
    187.      /** 
    188.  
    189.      * 从json数组中解析出javaLong型对象数组 
    190.  
    191.      * @param jsonString 
    192.  
    193.      * @return 
    194.  
    195.      */  
    196.   
    197.      public static Long[] getLongArray4Json(String jsonString){   
    198.   
    199.      JSONArray jsonArray = JSONArray.fromObject(jsonString);  
    200.   
    201.      Long[] longArray = new Long[jsonArray.size()];  
    202.   
    203.      forint i = 0 ; i<jsonArray.size() ; i++ ){  
    204.   
    205.      longArray[i] = Long.valueOf(String.valueOf(jsonArray.getLong(i)));  
    206.   
    207.       }  
    208.   
    209.      return longArray;  
    210.   
    211.      }  
    212.   
    213.        
    214.   
    215.      /** 
    216.  
    217.      * 从json数组中解析出java Integer型对象数组 
    218.  
    219.      * @param jsonString 
    220.  
    221.      * @return 
    222.  
    223.      */  
    224.   
    225.      public static Integer[] getIntegerArray4Json(String jsonString){   
    226.   
    227.      JSONArray jsonArray = JSONArray.fromObject(jsonString);  
    228.   
    229.      Integer[] integerArray = new Integer[jsonArray.size()];  
    230.   
    231.      forint i = 0 ; i<jsonArray.size() ; i++ ){  
    232.   
    233.      integerArray[i] = Integer.valueOf(String.valueOf(jsonArray.getInt(i)));  
    234.   
    235.       }  
    236.   
    237.      return integerArray;  
    238.   
    239.      }  
    240.   
    241.        
    242.   
    243.      /** 
    244.  
    245.      * 从json数组中解析出java Date 型对象数组,使用本方法必须保证 
    246.  
    247.      * @param jsonString 
    248.  
    249.      * @return 
    250.  
    251.      */  
    252.   
    253.      public static Date[] getDateArray4Json(String jsonString,String DataFormat){   
    254.   
    255.      JSONArray jsonArray = JSONArray.fromObject(jsonString);  
    256.   
    257.      Date[] dateArray = new Date[jsonArray.size()];  
    258.   
    259.      String dateString;  
    260.   
    261.      Date date;  
    262.   
    263.        
    264.   
    265.      forint i = 0 ; i<jsonArray.size() ; i++ ){  
    266.   
    267.      dateString = jsonArray.getString(i);  
    268.   
    269.     // date = DateUtil.stringToDate(dateString, DataFormat);  
    270.   
    271.     // dateArray[i] = date;   
    272.   
    273.      }  
    274.   
    275.      return dateArray;  
    276.   
    277.      }  
    278.   
    279.        
    280.   
    281.      /** 
    282.  
    283.      * 从json数组中解析出java Integer型对象数组 
    284.  
    285.      * @param jsonString 
    286.  
    287.      * @return 
    288.  
    289.      */  
    290.   
    291.      public static Double[] getDoubleArray4Json(String jsonString){   
    292.   
    293.      JSONArray jsonArray = JSONArray.fromObject(jsonString);  
    294.   
    295.      Double[] doubleArray = new Double[jsonArray.size()];  
    296.   
    297.      forint i = 0 ; i<jsonArray.size() ; i++ ){  
    298.   
    299.     /// doubleArray[i] = jsonArray.getDouble(i);   
    300.   
    301.      }  
    302.   
    303.      return doubleArray;  
    304.   
    305.      }  
    306.   
    307.        
    308.   
    309.      /** 
    310.  
    311.      * 将java对象转换成json字符串 
    312.  
    313.      * @param javaObj 
    314.  
    315.      * @return 
    316.  
    317.      */  
    318.   
    319.      public static String getJsonString4JavaPOJO(Object javaObj){   
    320.   
    321.      JSONObject json;  
    322.   
    323.      json = JSONObject.fromObject(javaObj);  
    324.   
    325.      return json.toString();  
    326.   
    327.       }  
    328.   
    329.        
    330.   
    331.      /** 
    332.  
    333.      * 将java对象转换成json字符串,并设定日期格式 
    334.  
    335.      * @param javaObj 
    336.  
    337.      * @param dataFormat 
    338.  
    339.      * @return 
    340.  
    341.      */  
    342.   
    343.      public static String getJsonString4JavaPOJO(Object javaObj , String dataFormat){   
    344.   
    345.      JSONObject json;  
    346.   
    347.      JsonConfig jsonConfig = configJson(dataFormat);  
    348.   
    349.      json = JSONObject.fromObject(javaObj,jsonConfig);  
    350.   
    351.      return json.toString();  
    352.      }  
    353.      /** 
    354.  
    355.       * JSON 时间解析器具 
    356.  
    357.       * @param datePattern 
    358.  
    359.       * @return 
    360.  
    361.       */  
    362.   
    363.       public static JsonConfig configJson(String datePattern) {   
    364.   
    365.       JsonConfig jsonConfig = new JsonConfig();   
    366.   
    367.       jsonConfig.setExcludes(new String[]{""});   
    368.   
    369.       jsonConfig.setIgnoreDefaultExcludes(false);   
    370.   
    371.       jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);   
    372.   
    373.       jsonConfig.registerJsonValueProcessor(Date.class,   
    374.   
    375.       new DateJsonValueProcessor(datePattern));    
    376.   
    377.       return jsonConfig;   
    378.   
    379.       }   
    380.   
    381.         
    382.   
    383.       /** 
    384.  
    385.       * 
    386.  
    387.       * @param excludes 
    388.  
    389.       * @param datePattern 
    390.  
    391.       * @return 
    392.  
    393.       */  
    394.   
    395.       public static JsonConfig configJson(String[] excludes,   
    396.   
    397.       String datePattern) {   
    398.   
    399.       JsonConfig jsonConfig = new JsonConfig();   
    400.   
    401.       jsonConfig.setExcludes(excludes);   
    402.   
    403.       jsonConfig.setIgnoreDefaultExcludes(false);   
    404.   
    405.       jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);   
    406.   
    407.       jsonConfig.registerJsonValueProcessor(Date.class,   
    408.   
    409.       new DateJsonValueProcessor(datePattern));    
    410.   
    411.       return jsonConfig;   
    412.   
    413.       }   
    414.         
    415.       public static JsonConfig configJson(String[] excludes  
    416.   
    417.              ) {   
    418.   
    419.               JsonConfig jsonConfig = new JsonConfig();   
    420.   
    421.               jsonConfig.setExcludes(excludes);   
    422.   
    423.               jsonConfig.setIgnoreDefaultExcludes(false);   
    424.   
    425.               jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);   
    426.   
    427.               
    428.               return jsonConfig;   
    429.   
    430.               }   
    431.   
    432. }  

     需要一个json的jar包

  • 相关阅读:
    react native错误排查-TypeError: window.deltaUrlToBlobUrl is not a function
    react native报错处理com.android.build.api.transform.TransformException: com.android.builder.dexing.DexArchiveBuilderException: com.android.builder.dexing.DexArchiveBuilderException: Failed to process
    react native中一次错误排查 Error:Error: Duplicate resources
    umijs开发实践-不同页面交叉使用dva中的modal文件导致的错误
    每天五分钟-javascript数据类型
    react native中使用echarts
    微信小程序中通过腾讯地图进行逆地址解析报错message: "请求来源未被授权, 此次请求来源域名:servicewechat.com"
    在react中实现打印功能
    mac git从代码仓库克隆代码,修改并上传
    基于jwt的用户登录认证
  • 原文地址:https://www.cnblogs.com/jpfss/p/8692721.html
Copyright © 2011-2022 走看看