Cara Ngonversi Objek Java menyang JSON

Ing tutorial iki, kita nuduhake cara ngowahi obyek Java menyang JSON nggunakake rong perpustakaan, Jackson lan Gson.

Kita bakal nggunakake kelas Person sing mung POJO standar. Sawise nggawe obyek wong, banjur bisa dikonversi dadi JSON nggunakake macem-macem perpustakaan.

Person.java


import java.util.List; import java.util.Map; public class Person {
String name;
Integer age;
List hobbies;
Map languages;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List getHobbies() {
return hobbies;
}
public void setHobbies(List hobbies) {
this.hobbies = hobbies;
}
public Map getLanguages() {
return languages;
}
public void setLanguages(Map languages) {
this.languages = languages;
} }


Konversi Java menyang JSON Nggunakake Jackson

Kita bisa nggunakake ObjectMapper kelas perpustakaan Jackson kanggo ngowahi obyek Java menyang JSON.

Sing ObjectMapper kelas duwe macem-macem cara kanggo nulis JSON kanggo ngajokake utawa nulis JSON minangka String.


Ing conto ing ngisor iki, kita nulis file lan uga nggunakake fungsi cetak sing cukup kanggo nampilake JSON sing wis diformat.



Nanging luwih dhisik, goleki perpustakaan Jackson saka Maven lan wenehi katergantungan ing pom.xml file:


com.fasterxml.jackson.core
jackson-core
2.10.2
com.fasterxml.jackson.core
jackson-databind
2.10.2

ConvertJavaObjectToJson.java

import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ConvertJavaObjectToJson {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
Person person = newPerson();

try {

mapper.writeValue(new File('person.json'), person);

String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(person);

System.out.println(jsonString);
} catch (Exception e) {

e.printStackTrace();
}
}
private static Person newPerson() {
Person person = new Person();

List hobbies = new ArrayList();
hobbies.add('Football');
hobbies.add('Cooking');
hobbies.add('Swimming');

Map languages = new HashMap();
languages.put('French', 'Beginner');
languages.put('German', 'Intermediate');
languages.put('Spanish', 'Advanced');

person.setName('David');
person.setAge(30);
person.setHobbies(hobbies);
person.setLanguages(languages);

return person;
} }

Output:


{ 'name' : 'David', 'age' : 30, 'hobbies' : [ 'Football', 'Cooking', 'Swimming' ], 'languages' : {
'French' : 'Beginner',
'German' : 'Intermediate',
'Spanish' : 'Advanced' } }

Uga file person.json digawe ing ruang kerja proyek.

Gegandhengan:



Konversi Java menyang JSON Nggunakake Gson

Entuk katergantungan


com.google.code.gson
gson
2.8.6

Banjur


import com.google.gson.Gson; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class ConvertJavaObjectToJson {
public static void main(String[] args) {
Gson gson = new Gson();
Person person = newPerson();
String jsonString = gson.toJson(person);
System.out.println(jsonString);
}
private static Person newPerson() {
Person person = new Person();

List hobbies = new ArrayList();
hobbies.add('Football');
hobbies.add('Cooking');
hobbies.add('Swimming');

Map languages = new HashMap();
languages.put('French', 'Beginner');
languages.put('German', 'Intermediate');
languages.put('Spanish', 'Advanced');

person.setName('David');
person.setAge(30);
person.setHobbies(hobbies);
person.setLanguages(languages);

return person;
} }

Output