zoukankan      html  css  js  c++  java
  • google gson使用

    关键字: json gson java

    Gson网址http://code.google.com/p/google-gson/

    1.简单的处理list和map

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    Gson gson = new Gson();
    List testList = new ArrayList();
    testList.add("first");
    testList.add("second");
    String listToJson = gson.toJson(testList);
    System.out.println(listToJson);     
    //prints ["first","second"]
      
    Map testMap = new HashMap();
    testMap.put("id", "id.first");
    testMap.put("name","name.second");
    String mapToJson = gson.toJson(testMap);
    System.out.println(mapToJson);  
    //prints {"id":"id.first","name":"name.second"}

     2.处理带泛型的集合

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    List<TestBean> testBeanList = new ArrayList<TestBean>();
    TestBean testBean = new TestBean();
    testBean.setId("id");
    testBean.setName("name");
    testBeanList.add(testBean);
              
    java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<TestBean>>() {
    }.getType();
    String beanListToJson = gson.toJson(testBeanList,type);
    System.out.println(beanListToJson);
    //prints [{"id":"id","name":"name"}]
              
    List<TestBean> testBeanListFromJson = gson.fromJson(beanListToJson, type);
    System.out.println(testBeanListFromJson);
    //prints [TestBean@1ea5671[id=id,name=name,birthday=<null>]]

    map等其他集合类型同上

    3.Date类型转化

    先写工具类

    工具类
    1 1. import java.lang.reflect.Type;
    2 2.
    3 3. import com.google.gson.JsonDeserializationContext;
    4 4. import com.google.gson.JsonDeserializer;
    5 5. import com.google.gson.JsonElement;
    6 6. import com.google.gson.JsonParseException;
    7 7.
    8 8. publicclass UtilDateDeserializer implements JsonDeserializer<java.util.Date> {
    9 9.
    10 10. @Override
    11 11. public java.util.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
    12 12. throws JsonParseException {
    13 13. returnnew java.util.Date(json.getAsJsonPrimitive().getAsLong());
    14 14. }
    15 15. }
    代码
    11. import java.lang.reflect.Type;
    22.
    33. import com.google.gson.JsonElement;
    44. import com.google.gson.JsonPrimitive;
    55. import com.google.gson.JsonSerializationContext;
    66. import com.google.gson.JsonSerializer;
    77.
    88. publicclass UtilDateSerializer implements JsonSerializer<java.util.Date> {
    99.
    1010. @Override
    1111. public JsonElement serialize(java.util.Date src, Type typeOfSrc,
    1212. JsonSerializationContext context) {
    1313. returnnew JsonPrimitive(src.getTime());
    1414. }
    1515.
    1616. }
    序列化
    11. /**
    2 2. * 序列化方法
    3 3. * @param bean
    4 4. * @param type
    5 5. * @return
    6 6. */
    77. publicstatic String bean2json(Object bean, Type type) {
    88. Gson gson =new GsonBuilder().registerTypeAdapter(java.util.Date.class, new UtilDateSerializer())
    99. .setDateFormat(DateFormat.LONG).create();
    1010. return gson.toJson(bean);
    1111. }
    1212.
    1313. /**
    14 14. * 反序列化方法
    15 15. * @param json
    16 16. * @param type
    17 17. * @return
    18 18. */
    1919. publicstatic<T> T json2bean(String json, Type type) {
    2020. Gson gson =new GsonBuilder().registerTypeAdapter(java.util.Date.class, new UtilDateDeserializer())
    2121. .setDateFormat(DateFormat.LONG).create();
    2222. return gson.fromJson(json, type);
    2323. }
    现在开始测试
    1# List<TestBean> testBeanList =new ArrayList<TestBean>();
    2# TestBean testBean =new TestBean();
    3# testBean.setId("id");
    4# testBean.setName("name");
    5# testBean.setBirthday(new java.util.Date());
    6# testBeanList.add(testBean);
    7#
    8# java.lang.reflect.Type type =new com.google.gson.reflect.TypeToken<List<TestBean>>() {
    9# }.getType();
    10# String beanListToJson = bean2json(testBeanList, type);
    11# System.out.println("beanListToJson:"+ beanListToJson);
    12# //prints [{"id":"id","name":"name","birthday":1256531559390}]
    13#
    14# List<TestBean> testBeanListFromJson = json2bean(beanListToJson, type);
    15# System.out.println(testBeanListFromJson);
    16# //prints [TestBean@77a7f9[id=id,name=name,birthday=Mon Oct 26 12:39:05 CST 2009]]
  • 相关阅读:
    zoj1151 zoj1295 Word Reversal 字符串的简单处理
    zoj 1539 Lot 简单DP 记忆化
    ZOJ 2042 Divisibility (DP)
    zoj 1889 ones 数学
    Kubernetes Ingress 日志分析与监控的最佳实践
    如何使用Data Lake Analytics创建分区表
    如何在Data Lake Analytics中使用临时表
    阿里敏捷教练:多团队开发一个产品的组织设计和思考
    阿里工程师开发了一款免费工具,提升Kubernetes应用开发效率
    触手可得的云原生 | 阿里云中间件发布多项新功能​
  • 原文地址:https://www.cnblogs.com/yangy608/p/2734300.html
Copyright © 2011-2022 走看看