zoukankan      html  css  js  c++  java
  • Json,Gson,FastJson解析笔记

    Json

    1.将JavaBean转换成Json对象:

    [java]  view plain  copy
     
    1. public static String CreatJsonFromObject(Object key,Object value)  
    2.     {  
    3.         JSONObject JObj =new JSONObject();  
    4.         JObj.put(key, value);  
    5.         return JObj.toString();  
    6.     }  

    2.解析Json:

    [html]  view plain  copy
     
    1. public static  Person parseJsonPerson(String key,String jsonStr)  
    2.   {  
    3.       System.out.print(key+"-->"+jsonStr);  
    4.       Person person =new Person();  
    5.       JSONObject jobj;  
    6.       try  
    7.       {  
    8.           jobj = new JSONObject(jsonStr);  
    9.           JSONObject personObj =jobj.getJSONObject(key);  
    10.           person.setAge(personObj.getInt("age"));  
    11.           person.setName(personObj.getString("name"));  
    12.           person.setSex(personObj.getString("sex"));  
    13.       }  
    14.       catch (JSONException e)  
    15.       {  
    16.           e.printStackTrace();  
    17.       }  
    18.       return person;  
    19.        
    20.   }  
    21.     
    22.   public static List<Person> parseJsonListPerson(String key,String jsonStr)  
    23.   {  
    24.       System.out.print(key+"-->"+jsonStr);  
    25.       List<Personlist=new ArrayList<Person>();  
    26.       try  
    27.       {  
    28.           JSONObject jobj;  
    29.           jobj = new JSONObject(jsonStr);  
    30.           JSONArray personArr =jobj.getJSONArray(key);  
    31.           for(int i=0;i<personArr.length();i++)  
    32.           {  
    33.               JSONObject personObj = personArr.getJSONObject(i);  
    34.               Person person =new Person();  
    35.               person.setAge(personObj.getInt("age"));  
    36.               person.setName(personObj.getString("name"));  
    37.               person.setSex(personObj.getString("sex"));  
    38.               list.add(person);  
    39.           }  
    40.       }  
    41.       catch (JSONException e)  
    42.       {  
    43.           // TODO Auto-generated catch block  
    44.           e.printStackTrace();  
    45.       }  
    46.       return list;  
    47.   }  
    48.     
    49.   public static List<String> parseJsonListString(String key,String jsonStr)  
    50.   {  
    51.       System.out.print(key+"-->"+jsonStr);  
    52.       List<Stringlist =new ArrayList<String>();  
    53.       try  
    54.       {  
    55.           JSONObject jobj;  
    56.           jobj = new JSONObject(jsonStr);  
    57.           JSONArray personArr =jobj.getJSONArray(key);  
    58.           for(int i=0;i<personArr.length();i++)  
    59.           {  
    60.               String personObj = personArr.getString(i);  
    61.               list.add(personObj);  
    62.           }  
    63.       }  
    64.       catch (JSONException e)  
    65.       {  
    66.           // TODO Auto-generated catch block  
    67.           e.printStackTrace();  
    68.       }  
    69.       return list;  
    70.   }  
    71.     
    72.   public static List<Map<String, Object>> parseJsonListMap(String key,String jsonStr)  
    73.   {  
    74.       System.out.print(key+"-->"+jsonStr);  
    75.       List<Map<String, Object>list =new ArrayList<Map<String,Object>>();  
    76.       try  
    77.       {  
    78.           JSONObject jobj;  
    79.           jobj = new JSONObject(jsonStr);  
    80.           JSONArray personArr =jobj.getJSONArray(key);  
    81.           for(int i=0;i<personArr.length();i++)  
    82.           {  
    83.               JSONObject personObj = personArr.getJSONObject(i);  
    84.               Iterator<Stringset =personObj.keys();  
    85.               while(set.hasNext())  
    86.               {  
    87.                   String tempKey =set.next();  
    88.                   Object tempValue =personObj.get(tempKey);  
    89.                   HashMap<String,Objectmap =new HashMap<String,Object>();  
    90.                   //注意tempValue可能为空  
    91.                   if(tempValue == null)  
    92.                   {  
    93.                       tempValue="";  
    94.                   }  
    95.                   map.put(tempKey, tempValue);  
    96.                   list.add(map);  
    97.               }  
    98.           }  
    99.             
    100.       }  
    101.       catch (JSONException e)  
    102.       {  
    103.           // TODO Auto-generated catch block  
    104.           e.printStackTrace();  
    105.       }  
    106.         
    107.         
    108.       return list;  
    109.   }  


    Gson:

    1.将JavaBean转换成Gson String

    [java]  view plain  copy
     
    1. public static String CreatJsonFromObject(Object value)  
    2. {  
    3.     Gson gson =new Gson();  
    4.     return gson.toJson(value);  
    5.       
    6. }  

    2.解析Gson String

    [java]  view plain  copy
     
    1. public static <T> T getPerson(String Json, Class<T> cls)  
    2.    {  
    3.        Gson gson = new Gson();  
    4.        T t = gson.fromJson(Json, cls);  
    5.        return t;  
    6.    }  
    7.      
    8.   public static <T> List<T> getPersons(String Json, Class<T> Type)  
    9.    {  
    10.        List<T> list = new Gson().fromJson(Json, new TypeToken<List<T>>()  
    11.        {  
    12.        }.getType());  
    13.        return list;  
    14.    }  
    15.      
    16.   public  static List<String> getString(String Json)  
    17.    {  
    18.        List<String> retStr = new Gson().fromJson(Json, new TypeToken<List<String>>()  
    19.        {  
    20.        }.getType());  
    21.        return retStr;  
    22.    }  
    23.      
    24.   public   static List<Map<String, Object>> getListMap(String Json)  
    25.    {  
    26.        List<Map<String, Object>> retStr = new Gson().fromJson(Json, new TypeToken<List<Map<String, Object>>>()  
    27.        {  
    28.        }.getType());  
    29.        return retStr;  
    30.    }  

    FastJson:

    1.将JavaBean转换成FastJson String

    服务端利用上述方式实现

    2.解析FastJson String

    [java]  view plain  copy
     
      1. public static <T> T getPerson(String jsonStr, Class<T> cls)  
      2.    {  
      3.        T t = JSON.parseObject(jsonStr, cls);  
      4.        return t;  
      5.    }  
      6.      
      7.    public static <T> List<T> getPersons(String jsonStr, Class<T> cls)  
      8.    {  
      9.        List<T> list = JSON.parseArray(jsonStr, cls);  
      10.        return list;  
      11.    }  
      12.      
      13.    /** 
      14.     * @param jsonStr 
      15.     * @return 
      16.     */  
      17.    public static List<Map<String, Object>> getListMap(String jsonStr)  
      18.    {  
      19.        List<Map<String, Object>> list = JSON.parseObject(jsonStr, new TypeReference<List<Map<String, Object>>>()  
      20.        {  
      21.        });  
      22.        return list;  
      23.    }  
      24. // from: http://www.voidcn.com/blog/zchlww/article/p-4999460.html
  • 相关阅读:
    少年中国说--正能量传播
    刚刚加入程序员的行列,希望通过博客的形式记录自己在这个领域的点点滴滴。同时分享自己的心得体会。
    java中的路径问题(getResourceAsStream/tomcat/maven/getContextpath)等各种路径问题
    java的jdbc
    maven的插件
    maven的仓库
    java9新特性
    java8新特性
    java的网络编程
    java的多线程juc
  • 原文地址:https://www.cnblogs.com/GarfieldEr007/p/6822311.html
Copyright © 2011-2022 走看看