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,同時

要更加注意這個框架,這個工具類所存在的問題,會有哪些坑!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM