示例
package org.example;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.Data;
public class TestPrettyPrint {
@Data
public static class UserInfo {
private String useName;
}
public static void main(String[] args) {
UserInfo userInfo = new UserInfo();
userInfo.setUseName("李白");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonStr = gson.toJson(userInfo);
System.out.println(jsonStr);
}
}
执行结果:
{
"useName": "李白"
}