如何在 Java 中创建 JSON 数组并写入文件?
Java 提供 javax.json.Json 包,其中包含用于读取 JSON 数组的类
示例
import java.io.FileOutputStream; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonWriter; public class JSONArrayToFile { public static void main(String args[]) throws Exception { JsonArray value = Json.createArrayBuilder() .add(Json.createObjectBuilder() .add("id", "1001") .add("Technology", "JavaFX")) .add(Json.createObjectBuilder() .add("id", "1002") .add("Technology", "OpenCV")) .build(); System.out.println(value); JsonWriter writer = Json.createWriter(new FileOutputStream("sampleData")); writer.writeArray(value); writer.close(); } }
输出
["JavaFX","HBase","JOGL","WebGL"] After deleting ::["JavaFX","HBase","JOGL"]
广告