[英]How to add JsonObjects to javax.json.JsonArray Using Loop (dynamically)
[英]How to add new JSONObjects to existing JSONArray in json file
我堅持將數據從文本字段保存在 json 文件中。 下面的這段代碼工作得很好,但是在重新啟動我的應用程序並嘗試添加新的 obj2 后,以前的數據正在刪除。 我嘗試使用 append,但隨后它被保存在 json 文件中的 [] 中。 我的第二個問題是漂亮地將我的數據寫入 json 文件,我無法在我的代碼中實現此方法,不知道為什么。 如果你知道如何解決它,我將不勝感激。
JSONObject obj1 = new JSONObject();
JSONObject obj2 = new JSONObject();
obj1.put("name", tfNameOfMedicine.getText());
obj1.put("amount",tfAmountOfMedicine.getText());
obj2.put("Medicine", obj1);
if(!checkIfInBase()) {
arr.add(obj2);
try (FileWriter Data = new FileWriter("Data.JSON")) {
Data.write(arr.toJSONString());
Data.flush();
JOptionPane.showMessageDialog(null, "Saved!");
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error");
}
}else{JOptionPane.showMessageDialog(null, "This medicine exist in data base");}
tfAmountOfMedicine.setText("");
tfNameOfMedicine.setText("");
}
根據 Alex Rudenko 的評論: 1.我不知道我是否重新閱讀了 arr,我在私有聲明下面這樣創建它。
JSONArray arr = new JSONArray();
2.這是我的檢查方法。
private boolean checkIfInBase() {
JSONObject obj1 = new JSONObject();
JSONObject obj2 = new JSONObject();
int size = arr.size();
obj1.put("name", createMedicine().getName());
obj1.put("amount", createMedicine().getAmount());
obj2.put("Medicine", obj1);
boolean a = false;
for (int i = 0; i < size; i++) {
if (obj1.equals(arr.get(i))) {
a = true;
break;
}
}
return a;
}
3. 我正在使用 json.simple
json.simple 很可能沒有漂亮的打印功能,而且它很舊,所以我建議切換到任何其他庫。 在下面的示例中,我將使用org.json
您需要重新讀取 json 文件並初始化arr
變量。
您可以通過將數組中的 JSON 對象轉換為字符串並進行比較來檢查它們的相等性。
修復 JSON 文件的重寫。
import java.io.*;
import org.json.JSONTokener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonExample {
private static JSONArray initArray(String jsonFileName) throws IOException, JSONException {
String json = "";
File jsonFile = new File(jsonFileName);
if (!jsonFile.exists()) {
return new JSONArray();
}
try (BufferedReader reader = new BufferedReader(new FileReader(jsonFile))) {
String line;
while ((line = reader.readLine()) != null) {
json += line;
}
}
JSONTokener tokener = new JSONTokener(json);
JSONArray readArr = new JSONArray(tokener);
System.out.println("Read arr: " + readArr.toString());
return readArr;
}
private static boolean isInDB(JSONObject obj, JSONArray arr) throws JSONException {
for (int i = 0; i < arr.length(); i++) {
JSONObject item = arr.getJSONObject(i);
if (obj.toString().equals(item.toString())) {
return true;
}
}
return false;
}
private static JSONArray saveArray(JSONArray arr, String jsonFileName) throws JSONException, IOException {
try (FileWriter Data = new FileWriter(jsonFileName)) {
Data.write(arr.toString(4)); // setting spaces for indent
}
return arr;
}
// main method
public static void main(String[] args) throws Exception {
JSONArray arr = initArray("DataMedicine.json");
JSONObject obj = new JSONObject();
obj.put("name", "Antivirin");
obj.put("amount", 15);
JSONObject medicine = new JSONObject();
medicine.put("medicine3", obj);
if (!isInDB(medicine, arr)) {
arr.put(medicine);
saveArray(arr, "DataMedicine.json");
} else {
System.out.println("Already exists in DB: " + obj);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.