fastJson中常用JSON字符串和Java对象互转
发布时间:2020-12-16 18:42:45 所属栏目:百科 来源:网络整理
导读:1.使用fastJson,首先引入fastJson依赖 !-- https: // mvnrepository.com/artifact/com.alibaba/fastjson -- dependency groupIdcom.alibaba/groupId artifactIdfastjson/artifactId version1.2.54/version/dependency 2.JSON String to Java Bean /** * JSO
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);
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); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |