Gson Google에서 만든 Java용 JSON 라이브러리입니다. Gson을 사용하여 JSON을 생성할 수 있습니다. JSON을 Java 객체로 변환합니다. 기본적으로 Gson은 JSON을 간단한 형식으로 인쇄할 수 있습니다. . Gson 프리티 프린트를 활성화하려면 , setPrettyPrinting()을 사용하여 Gson 인스턴스를 구성해야 합니다. GsonBuilder 메소드 class이며 이 메소드는 예쁜 인쇄를 위해 페이지에 맞는 JSON을 출력하도록 Gson을 구성합니다.
구문
public GsonBuilder setPrettyPrinting()
예시
import java.util.*;
import com.google.gson.*;
public class PrettyJSONTest {
public static void main( String[] args ) {
Employee emp = new Employee("Raja", "115", "Content Engineer", "Java", "Hyderabad");
Gson gson = new GsonBuilder().setPrettyPrinting().create(); // pretty print
String prettyJson = gson.toJson(emp);
System.out.println(prettyJson);
}
}
// Employee class
class Employee {
private String name, id, designation, technology, location;
public Employee(String name, String id, String designation, String technology, String location) {
super();
this.name = name;
this.id = id;
this.designation = designation;
this.technology = technology;
this.location = location;
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getDesignation() {
return designation;
}
public String getTechnology() {
return technology;
}
public String getLocation() {
return location;
}
} 출력
{
"name": "Raja",
"id": "115",
"designation": "Content Engineer",
"technology": "Java",
"location": "Hyderabad"
}