द फ्लेक्सजसन एक हल्का . है क्रमबद्ध करने . के लिए जावा लाइब्रेरी और डी-सीरियलाइज़ करना जावा बीन्स, मानचित्र, सरणियाँ , और संग्रह एक JSON . में प्रारूप। एक JSONSerializer JSON के लिए जावा ऑब्जेक्ट का क्रमांकन करने के लिए मुख्य वर्ग है और डिफ़ॉल्ट रूप से एक उथला करता है क्रमबद्धता . हम सुंदर प्रिंट . कर सकते हैं JSON prettyPrint(boolean beautifulPrint) . का उपयोग कर रहा है JSONSerializer . की विधि कक्षा।
सिंटैक्स
public JSONSerializer prettyPrint(boolean prettyPrint)
नीचे दिए गए प्रोग्राम में, सुंदर प्रिंट JSON फ्लेक्सजसन लाइब्रेरी का उपयोग करना
उदाहरण
import flexjson.*;
public class PrettyPrintJSONTest {
public static void main(String[] args) {
JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print
Employee emp = new Employee("Vamsi", "105", "Python Developer", "Python", "Pune");
String jsonStr = serializer.serialize(emp);
System.out.println(jsonStr);
}
}
// 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;
}
} आउटपुट
{
"class": "Employee",
"designation": "Python Developer",
"id": "105",
"location": "Pune",
"name": "Vamsi",
"technology": "Python"
}