我想在运行时从 Java GUI 将元素添加到 JSON 数组
但每次在 JSON 文件中创建新数组时
用于输入数据的 Java GUI:
String _itemType = txtItemType.getText();
int _itemQuantity = Integer.parseInt(txtItemQuantity.getText());
JSONWriteExample obj = new JSONWriteExample(_itemType, _itemQuantity);
obj.jsonParse();
JSON:
public JSONWriteExample(String type, int number) {
this.type = type;
this.quantity = number;
}
public void jsonParse() throws IOException {
JSONObject jo = new JSONObject();
Map m = new LinkedHashMap(4);
JSONArray ja = new JSONArray();
m = new LinkedHashMap(2);
m.put("Item Type", type);
m.put("Quantity", quantity);
ja.add(m);
jo.put("Items", ja);
FileWriter file=new FileWriter("jsonArray.json",true);
file.append(jo.toString());
file.flush();
file.close();
}
我期望的输出如下:
{
"Items":[
{
"Item Type":"TV",
"Quantity":3
},
{
"Item Type":"phone",
"Quantity":3
}
]
}
但是每次都会创建新数组,例如:
{
"Items":[
{
"Item Type":"TV",
"Quantity":3
}
]
}{
"Items":[
{
"Item Type":"phone",
"Quantity":3
}
]
}
最佳答案
正如 @fabian 在评论中提到的 - 你应该首先解析文件内容,修改并覆盖文件。以下是如何实现这一目标的示例代码:
首先,我不知道你正在使用什么json库,但我强烈建议使用以下库:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
它通常会简化您使用 json 的工作。如果您不想使用库,您仍然可以按照说明进行操作,但请根据您的需要进行调整。整个实现是这样的:
public class JSONWriteExample {
private static final String FILE_NAME = "jsonArray.json";
private static final Path FILE_PATH = Paths.get(FILE_NAME);
private final String type;
private final int quantity;
public JSONWriteExample(String type, int quantity) {
this.type = type;
this.quantity = quantity;
}
public void jsonParse() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
if (Files.notExists(FILE_PATH)) {
Files.createFile(FILE_PATH);
objectMapper.writeValue(FILE_PATH.toFile(), createItems(new ArrayList<>()));
}
Items items = objectMapper.readValue(FILE_PATH.toFile(), Items.class);
final List<Item> itemsList = items.getItems();
objectMapper.writeValue(FILE_PATH.toFile(), createItems(itemsList));
}
private Items createItems(List<Item> itemsList) {
final Item item = new Item();
item.setType(type);
item.setQuantity(quantity);
itemsList.add(item);
final Items items = new Items();
items.setItems(itemsList);
return items;
}
public static class Items {
private List<Item> items;
// Setters, Getters
}
public static class Item {
private String type;
private int quantity;
// Setters, Getters
}
}
好的,这段代码发生了什么?
- 首先,请注意 Java 7 NIO 的用法 - 在 Java 中处理文件的推荐方法。
- 在
jsonParse
方法中,我们首先检查文件是否存在。- 如果是,那么我们将其读入描述我们模型的数据类 (
Items
)。读取部分是在这个库的引擎盖下完成的,只是 json 文件的字段应该与数据类的字段具有相同的名称(或使用JsonAlias
注释指定。 - 如果没有 - 那么我们首先创建它并填充初始值。
- 如果是,那么我们将其读入描述我们模型的数据类 (
ObjectMapper
是库中的类,用于读取\写入 json 文件。
现在,如果我们运行这段代码,例如
public static void main(String[] args) throws IOException {
JSONWriteExample example = new JSONWriteExample("TV", 3);
example.jsonParse();
JSONWriteExample example2 = new JSONWriteExample("phone", 3);
example2.jsonParse();
}
json 文件将如下所示:
{
"items": [
{
"type": "TV",
"quantity": 3
},
{
"type": "TV",
"quantity": 3
},
{
"type": "phone",
"quantity": 3
}
]
}
关于java - 如何在运行时从 Java 接口(interface)向 json 数组添加元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54747234/