fastJson中常用方法以及遇到的“坑”

1.使用fastJson,首先引入fastJson依赖

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.54</version>
</dependency>

2.JSON String to Java Bean

/**
 * JSON->Java Bean
 */
@Test
public void test1(){
    Person person = new Person().setId("1").setName("fastJson").setAge(1);
    String jsonString = JSON.toJSONString(person);
    System.out.println(jsonString);
//传入字节码,调用parseObject Person person1
= JSON.parseObject(jsonString, Person.class); System.out.println(person1); }

3.JSON String to List Java Bean

/**
 * JSON->List Java Bean
 */
@Test
public void test2(){
    List<Person> list = Arrays.asList(new Person().setId("1").setName("fastJson1").setAge(1),
            new Person().setId("2").setName("fastJson2").setAge(2),
            new Person().setId("3").setName("fastJson3").setAge(3));
    String jsonString = JSON.toJSONString(list);
    System.out.println(jsonString);
//传入字节码,调用parseArray List
<Person> person1 = JSON.parseArray(jsonString, Person.class); System.out.println(person1); }

4.JSON String to List String

/**
 * JSON->List String
 */
@Test
public void test3(){
    List<String> list = Arrays.asList("hello","world","hello world");
    String jsonString = JSON.toJSONString(list);
    System.out.println(jsonString);
   // new TypeReference<List<String>>() {} List
<String> list1 = JSON.parseObject(jsonString, new TypeReference<List<String>>() {}); System.out.println(list1); }

5.JSON String to List<Map<String,Object>>

/**
 * JSON->List<Map<String,Object>>
 */
@Test
public void test4(){
    List<Map<String,Object>> list = new ArrayList<>();
    Map<String,Object> map = new HashMap<>();
    map.put("key1","value1");
    map.put("key2","value2");
    list.add(map);
    Map<String,Object> map1 = new HashMap<>();
    map1.put("key11","value11");
    map1.put("key22","value22");
    list.add(map1);
    Map<String,Object> map2 = new HashMap<>();
    map2.put("key111","value111");
    map2.put("key222","value222");
    list.add(map2);
    String jsonString = JSON.toJSONString(list);
    System.out.println(jsonString);
  // new TypeReference<T> List
<Map<String, Object>> maps = JSON.parseObject(jsonString, new TypeReference<List<Map<String, Object>>>() {}); System.out.println(maps); }

 6.JSON.toJSONString中序列化map中的空字符串会出现空对象问题

@Test
public void testJSON() throws Exception {
	Map<String,String> map = new HashMap<String, String>();
	map.put("aaa",null);
	map.put("bbb",null);
	map.put("ccc",null);
	System.out.println(map);
	String s = JSON.toJSONString(map);
	System.out.println(s);
	Map<String, String> stringMap = JSON.parseObject(s, new TypeReference<Map<String, String>>() {
	});
	System.out.println(stringMap);
}

上面的代码需要经过如下修改,才不会出现空对象问题

String s = JSON.toJSONString(map, SerializerFeature.WriteMapNullValue);

7.总结:如果大家在使用fastJSON序列化时出现问题,可以考虑朝着序列化这个方向考虑问题,多了解了解SerializerFeature。我们在学习并使用某一项api时,不仅仅要会这个api,同时

要更加注意这个框架,这个工具类所存在的问题,会有哪些坑!

posted @ 2018-12-23 23:14  crazy-zz5536  阅读(2849)  评论(0编辑  收藏  举报