当前位置:   article > 正文

FastJSON 简单使用_parseobject parserconfig

parseobject parserconfig

转载链接:http://blog.csdn.net/flysun3344/article/details/54707965

需要相关jar包,如果使用maven的话,在pom.xml文件加入一下依赖:

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>1.2.46</version>
  5. </dependency>

User.java

  1. package com.accord.fastjson;
  2. public class User {
  3. private String userName;
  4. private Integer age;
  5. public User() {
  6. super();
  7. }
  8. public User(String userName, Integer age) {
  9. super();
  10. this.userName = userName;
  11. this.age = age;
  12. }
  13. public String getUserName() {
  14. return userName;
  15. }
  16. public void setUserName(String userName) {
  17. this.userName = userName;
  18. }
  19. public Integer getAge() {
  20. return age;
  21. }
  22. public void setAge(Integer age) {
  23. this.age = age;
  24. }
  25. }
FastJson.java

  1. package com.accord.fastjson;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import com.alibaba.fastjson.JSON;
  8. import com.alibaba.fastjson.JSONArray;
  9. import com.alibaba.fastjson.JSONObject;
  10. import com.alibaba.fastjson.TypeReference;
  11. import com.alibaba.fastjson.parser.ParserConfig;
  12. import com.alibaba.fastjson.serializer.SerializerFeature;
  13. public class FastJson {
  14. public static void main(String[] args) {
  15. //基本的序列化
  16. Map<String,Object> map = new HashMap<String, Object>();
  17. map.put("key1", "one");
  18. map.put("key2", "two");
  19. String mapJson = JSON.toJSONString(map);
  20. System.out.println(mapJson);
  21. //将List<Map>转成JSON
  22. List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
  23. Map<String,Object> map1 = new HashMap<String, Object>();
  24. map1.put("key1", "One");
  25. map1.put("key2", "Two");
  26. Map<String,Object> map2 = new HashMap<String, Object>();
  27. map2.put("key1", "Three");
  28. map2.put("key2", "Four");
  29. list.add(map1);
  30. list.add(map2);
  31. String listJson = JSON.toJSONString(list);
  32. System.out.println(listJson);
  33. //自定义JavaBean User 转成Json
  34. User user = new User();
  35. user.setUserName("wjy");
  36. user.setAge(25);
  37. String userJson = JSON.toJSONString(user);
  38. System.out.println(userJson);
  39. //可以输出格式化后的JSON字符串
  40. //传入一个对象和一个布尔类型(是否格式化),将对象转成格式化后的JSON字符串。
  41. List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
  42. Map<String,Object> map1 = new HashMap<String, Object>();
  43. map1.put("key1", "One");
  44. map1.put("key2", "Two");
  45. Map<String,Object> map2 = new HashMap<String, Object>();
  46. map2.put("key1", "Three");
  47. map2.put("key2", "Four");
  48. list.add(map1);
  49. list.add(map2);
  50. String listJson = JSON.toJSONString(list,true);
  51. System.out.println(listJson);
  52. //日期格式化:
  53. //FastJSON可以直接对日期类型格式化,在缺省的情况下,FastJSON会将Date转成long
  54. String dataJson = JSON.toJSONString(new Date());
  55. System.out.println(dataJson); //结果:1401370199040
  56. //使用SerializerFeature特性格式化日期。
  57. String dateJson = JSON.toJSONString(new Date(),SerializerFeature.WriteDateUseDateFormat);
  58. System.out.println(dateJson); //结果:"2018-02-09 09:14:58"
  59. //指定输出日期格式
  60. String dateJson = JSON.toJSONStringWithDateFormat(new Date(),"yyyy-MM-dd");
  61. System.out.println(dateJson); //结果:"2018-02-09"
  62. //使用单引号
  63. List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
  64. Map<String,Object> map1 = new HashMap<String, Object>();
  65. map1.put("key1", "One");
  66. map1.put("key2", "Two");
  67. Map<String,Object> map2 = new HashMap<String, Object>();
  68. map2.put("key1", "Three");
  69. map2.put("key2", "Four");
  70. list.add(map1);
  71. list.add(map2);
  72. //String listJson = JSON.toJSONString(list,SerializerFeature.UseSingleQuotes);
  73. //System.out.println(listJson); //结果:[{'key2':'Two','key1':'One'},{'key2':'Four','key1':'Three'}]
  74. String listJson = JSON.toJSONString(list,SerializerFeature.PrettyFormat);
  75. System.out.println(listJson); //结果是带有格式的
  76. //输出Null字段。
  77. //缺省情况下FastJSON不输入为值Null的字段,可以使用SerializerFeature.WriteMapNullValue使其输出。
  78. Map<String, Object> map = new HashMap<String, Object>();
  79. String b = null;
  80. Integer i = 1;
  81. map.put("a", b);
  82. map.put("b", i);
  83. String listJson = JSON.toJSONString(map,SerializerFeature.WriteMapNullValue);
  84. System.out.println(listJson); //结果:{"b":1,"a":null}
  85. //序列化是写入类型信息
  86. User user = new User();
  87. user.setAge(26);
  88. user.setUserName("wjy");
  89. String listJson = JSON.toJSONString(user,SerializerFeature.WriteClassName);
  90. System.out.println(listJson); //结果:{"@type":"com.accord.fastjson.User","age":26,"userName":"wjy"}
  91. //将上面的反序列化
  92. ParserConfig.getGlobalInstance().setAutoTypeSupport(true); //不加这句会报错:
  93. /*Exception in thread "main" com.alibaba.fastjson.JSONException: autoType is not support. com.accord.fastjson.User
  94. at com.alibaba.fastjson.parser.ParserConfig.checkAutoType(ParserConfig.java:1026)
  95. at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:316)
  96. at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1356)
  97. at com.alibaba.fastjson.parser.DefaultJSONParser.parse(DefaultJSONParser.java:1322)
  98. at com.alibaba.fastjson.JSON.parse(JSON.java:152)
  99. at com.alibaba.fastjson.JSON.parse(JSON.java:162)
  100. at com.alibaba.fastjson.JSON.parse(JSON.java:131)
  101. at com.accord.fastjson.FastJson.main(FastJson.java:111)*/
  102. User user1 = (User)JSON.parse(listJson);
  103. System.out.println(user1.getUserName()); //结果:wjy
  104. //指定class信息反序列化
  105. User user = new User();
  106. user.setUserName("李四");
  107. user.setAge(24);
  108. String userJson = JSON.toJSONString(user);
  109. User user1 = JSON.parseObject(userJson,User.class);
  110. System.out.println(user1.getUserName());
  111. //集合反序列化
  112. List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  113. Map<String, Object> map1 = new HashMap<String, Object>();
  114. map1.put("key1", "One");
  115. map1.put("key2", "Two");
  116. Map<String, Object> map2 = new HashMap<String, Object>();
  117. map2.put("key1", "Three");
  118. map2.put("key2", "Four");
  119. list.add(map1);
  120. list.add(map2);
  121. String listJson = JSON.toJSONString(list);
  122. List<Map> list1 = JSON.parseArray(listJson, Map.class);
  123. for (Map<String,Object> map : list1) {
  124. System.out.println(map.get("key1"));
  125. System.out.println(map.get("key2"));
  126. }
  127. //泛型的反序列化(使用TypeReference传入类型信息)
  128. Map<String, Object> map = new HashMap<String, Object>();
  129. map.put("key1", "One");
  130. map.put("key2", "Two");
  131. String mapJson = JSON.toJSONString(map);
  132. Map<String,Object> map1 = JSON.parseObject(mapJson,new TypeReference<Map<String,Object>>(){});
  133. System.out.println(map1.get("key1"));
  134. System.out.println(map1.get("key2"));
  135. //将Map转成JSONObject,然后添加元素,输出
  136. Map<String,Object> map = new HashMap<String, Object>();
  137. map.put("key1", "One");
  138. map.put("key2", "Two");
  139. JSONObject j = new JSONObject(map);
  140. j.put("key3", "Three");
  141. System.out.println(j.get("key1"));
  142. System.out.println(j.get("key2"));
  143. System.out.println(j.get("key3"));
  144. //将List对象转成JSONArray,然后输出
  145. List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
  146. Map<String, Object> map = new HashMap<String, Object>();
  147. map.put("key1", "One");
  148. map.put("key2", "Two");
  149. Map<String, Object> map2 = new HashMap<String, Object>();
  150. map2.put("key1", "Three");
  151. map2.put("key2", "Four");
  152. list.add(map);
  153. list.add(map2);
  154. JSONArray j = JSONArray.parseArray(JSON.toJSONString(list));
  155. for (int i = 0; i < j.size(); i++) {
  156. System.out.println(j.get(i));
  157. }
  158. }
  159. }

MyJsonUtil.java

  1. package com.accord.fastjson;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.serializer.SerializerFeature;
  4. public class MyJsonUtil {
  5. private static final SerializerFeature[] features = {
  6. SerializerFeature.WriteMapNullValue, // 输出空置字段
  7. SerializerFeature.WriteNullListAsEmpty,
  8. // // list字段如果为null,输出为[],而不是null
  9. SerializerFeature.WriteNullNumberAsZero,
  10. // // 数值字段如果为null,输出为0,而不是null
  11. SerializerFeature.WriteNullBooleanAsFalse,
  12. // // Boolean字段如果为null,输出为false,而不是null
  13. SerializerFeature.WriteNullStringAsEmpty,
  14. // // 字符类型字段如果为null,输出为"",而不是null
  15. SerializerFeature.WriteDateUseDateFormat // 日期格式化yyyy-MM-dd HH:mm:ss
  16. };
  17. public static String toJson(Object object) {
  18. return JSON.toJSONString(object, features);
  19. }
  20. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/923029
推荐阅读
相关标签
  

闽ICP备14008679号