javascript
Java追加写json_java – ObjectMapper追加文件JSON
試圖了解一些杰克遜,所以我正在編寫一個簡單的程序來讀取文件/創建一個文件來存儲一些JSON.在Jackson網站上,我想出了如何從文件中讀取和寫入,但在我的基本程序中,我也想附加.我基本上試圖存儲一個購物清單列表.有一個購物清單對象,其中包含商店名稱,該商店的amd商品.
麻煩的是我無法找到將另一個條目追加到文件末尾的方法(采用JSON格式).這是我到目前為止所使用的,您可以忽略它的第一位,它只是一個愚蠢的控制臺掃描儀要求輸入:
public class JacksonExample {
static ObjectMapper mapper = new ObjectMapper();
static File file = new File("C:/Users/stephen.protzman/Desktop/user.json");
static List master = new ArrayList();
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("[ 1 ] Add a new shopping list");
System.out.println("[ 2 ] View all shopping lists");
System.out.println("[ 3 ] Save all shopping lists");
int choice = Integer.parseInt(in.nextLine());
switch (choice) {
case 1:
getNewList();
case 2:
display();
case 3:
running = false;
}
}
in.close();
}
public static void getNewList() {
boolean more = true;
String store, temp;
List items = new ArrayList();
Scanner s = new Scanner(System.in);
System.out.println("Enter the store: ");
store = s.nextLine();
System.out.println("Enter each item [If done type 'DONE'] :");
while (more) {
temp = s.nextLine();
if (temp != null) {
if (temp.toUpperCase().equals("DONE")) {
more = false;
} else {
items.add(temp);
}
}
}
save(store, items);
s.close();
}
public static void display() {
try {
ShoppingList list = mapper.readValue(file, ShoppingList.class);
System.out.println(mapper.defaultPrettyPrintingWriter()
.writeValueAsString(list));
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void save(String store, List items) {
//load in old one
try {
ShoppingList list = mapper.readValue(file, ShoppingList.class);
System.out.println(mapper.defaultPrettyPrintingWriter()
.writeValueAsString(list));
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//add to end of older list
ShoppingList tempList = new ShoppingList();
tempList.setStore(store);
tempList.setItems(items);
master.add(tempList);
try {
mapper.writeValue(file, master);
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我想繼續使用ObjectMapper(考慮到我試圖學習杰克遜)我還沒有找到一種方法來追加即可.有任何想法嗎?
解決方法:
要附加內容,您需要使用Streaming API來創建JsonGenerator;然后你可以把這個生成器給ObjectMapper寫入.所以類似于:
JsonGenerator g = mapper.getFactory().createGenerator(outputStream);
mapper.writeValue(g, valueToWrite);
// and more
g.close();
標簽:json,java,file,io,jackson
來源: https://codeday.me/bug/20190612/1223221.html
總結
以上是生活随笔為你收集整理的Java追加写json_java – ObjectMapper追加文件JSON的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java中hashcode作用_Java
- 下一篇: 干翻Java_Java第三次作业第一题