Gson เป็นไลบรารี JSON สำหรับ Java ใช้สร้าง JSON ในขั้นตอนแรก เราอ่านไฟล์ JSON และแปลงเป็นอ็อบเจกต์ Java แล้ว typecast อ็อบเจกต์ Java เป็น JsonObject และแปลงเป็น JsonArray จากนั้นวนลูป JSON array นี้เพื่อพิมพ์ JsonElement เราสามารถสร้างคลาส JsonWriter เพื่อเขียนค่า JSON ที่เข้ารหัสแล้วไปยังสตรีม ทีละโทเค็น สุดท้าย สตริง JSON ใหม่สามารถเขียนลงในไฟล์ JSON ที่มีอยู่แล้วได้
ตัวอย่างโค้ด
import java.io.*;
import java.util.*;
import com.google.gson.*;
import com.google.gson.stream.*;
import com.google.gson.annotations.*;
public class JSONFilewriteTest {
public static String nameRead;
public static void main(String[] args) {
try {
JsonParser parser = new JsonParser();
Object obj = parser.parse(new FileReader("employee1.json"));
JsonObject jsonObject = (JsonObject) obj;
System.out.println("The values of employee1.json file:\n" + jsonObject);
JsonArray msg = (JsonArray) jsonObject.get("emps");
Iterator<JsonElement> iterator = msg.iterator();
while(iterator.hasNext()) {
nameRead = iterator.next().toString();
System.out.println(nameRead);
}
Name name = new Name();
name.setName("Vamsi");
Gson gson = new Gson();
String json = gson.toJson(name);
FileWriter file = new FileWriter("employee1.json", false);
JsonWriter jw = new JsonWriter(file);
iterator = msg.iterator();
Employees emps = new Employees();
while(iterator.hasNext()) {
emps.addEmployee(gson.fromJson(iterator.next().toString(), Name.class));
}
emps.addEmployee(name);
gson.toJson(emps, Employees.class, jw);
file.close();
System.out.println("data added to an existing employee1.json file successfully");
} catch(Exception e) {
e.printStackTrace();
}
}
}
// คลาส Name
class Name {
@Expose
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
// คลาส Employees
class Employees {
@Expose
List<Name> emps = new ArrayList<>();
public List<Name> getEmployees() { return emps; }
public void addEmployee(Name name) { this.emps.add(name); }
}
ผลลัพธ์
The values of employee1.json file: {"emps":[{"name":"Adithya"},{"name":"Jai"}]}
{"name":"Adithya"}
{"name":"Jai"}
data added to an existing employee1.json file successfully
ไฟล์ employee1.json
