1.maven配置
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.41</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
2.demo代码
/** * */ package jsontest; import com.alibaba.fastjson.JSON; import com.google.gson.Gson; /** * @author jhuang.sjtu * @function null都会过滤掉 * @date 2017年12月10日 * @version */ public class TestJson { /** * @param args */ public static void main(String[] args) { //对象转化为json User use = new User("admin", null, 20); String ss = new Gson().toJson(use); System.out.println(ss); //json转化为对象 User use222 = new Gson().fromJson(ss, User.class); System.out.println("use222"+use222.toString()); System.out.println("以上为方法1,以下为方法2==================="); // 用户组对象转JSON串 String jsonString = JSON.toJSONString(use); System.out.println("jsonString:" + jsonString); // JSON串转用户组对象 User group2 = JSON.parseObject(jsonString, User.class); System.out.println("group2:" + group2); } }