I'm trying to append JSON Object each time within an existing JSON Array. For that i'm using GSON libraries. Tried below code :
OrderIDDetailBean ord = new OrderIDDetailBean();
ord.uniqueID = "sadasdas0w021";
ord.orderID = "Nand";
ord.cartTotal = "50";
ord.date = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS").format(new Date());
try {
JsonWriter writer = new JsonWriter(new FileWriter("D:\\file.json", true));
writer.beginArray();
writer.beginObject();
writer.name("uniqueID").value(ord.uniqueID);
writer.name("orderID").value(ord.orderID);
writer.name("cartTotal").value(ord.cartTotal);
writer.name("date").value(ord.date);
writer.endObject();
writer.endArray();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
But it created JSON array each time instead of appending.
Actual:
[
{
"uniqueID":"CHECKOUT_ES01",
"orderID":"5001787761",
"date":"07-02-2019 15:31:41.637",
"cartTotal":"11.44"
}
]
[
{
"uniqueID":"CHECKOUT_ES01",
"orderID":"5001787767",
"date":"07-02-2019 15:35:20.347",
"cartTotal":"11.44"
}
]
Expected :
[
{
"uniqueID":"CHECKOUT_ES01",
"orderID":"5001787761",
"date":"07-02-2019 15:31:41.637",
"cartTotal":"11.44"
},
{
"uniqueID":"CHECKOUT_ES01",
"orderID":"5001787767",
"date":"07-02-2019 15:35:20.347",
"cartTotal":"11.44"
}
]
Any help would be great appreciated.
new FileWriter("D:\\file.json", false), thetrueyou're currently using means what you writes will be appended at the end of the existing content.true