0
問題我有一個問題,Gson將集合更改爲Json對象。這是我的代碼:gson.toJson(Collection)
Collection<BomItem> items = bh.getAllItems();
Iterator<BomItem> itemIterator = items.iterator();
Collection<JsonItem> jsonItems = new Vector<JsonItem>();
JsonItem jsonItem = new JsonItem();
while(itemIterator.hasNext()) {
BomItem item = itemIterator.next();
jsonItem.setItemId(item.getItemId());
jsonItem.setName(item.getDescription());
jsonItems.add(jsonItem);
}
System.out.println("json size:" + jsonItems.size());
Gson gson = new Gson();
String json = gson.toJson(jsonItems);
System.out.println("Json: " + json);
這是輸出:
09:36:13,846 INFO [STDOUT] json size:402
09:36:13,849 INFO [STDOUT] Json: [{"itemId":68073,"name":"Automatically inserted"},{"itemId":68073,"name":"Automatically inserted"},{"itemId":68073,"name":"Automatically inserted"},...}
的JsonString被創建,但只有最新的對象,一遍又一遍。我究竟做錯了什麼?
嗯,這是尷尬。它的工作,非常感謝,我會盡快接受答案停止接受! – doonot