如何在json結構中追加行?輸入JSON由用戶提供爲html請求,每當我們需要添加日期或任何其他功能時新的JSON並返回爲html響應。如何在請求消息中添加新行並作爲回覆發回
例如: 如果我的JSON文件是:{名稱:ABC,年齡:12}然後我的新的輸出JSON將{名稱:ABC,年齡:12,日期:26/09/2017}
CODE:
InputStream inputStream = request.getInputStream();
String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
JsonObject convertedObject = new Gson().fromJson(text, JsonObject.class);
convertedObject.addProperty("Date", "26/09/2017");
PrintWriter pw = response.getWriter();
pw.println(convertedObject.toString());
通過上面的代碼等植物學JSON被刪除只有日期顯示:(
你可以嘗試convertying JSON來圖,來添加日期在該地圖上,並轉換回到json。看看這個https://stackoverflow.com/questions/443499/convert-json-to-map – mlecz