代码示例
package com.example;
import com.google.gson.Gson;
import lombok.Data;
public class Main01 {
@Data
public static class UserInfo {
private String name;
}
public static void main(String[] args) {
Gson gson = new Gson();
UserInfo userInfo = new UserInfo();
userInfo.setName("李白");
String jsonStr = gson.toJson(userInfo);
System.out.println(jsonStr);
}
}
执行结果:
{"name":"李白"}