使用Java在现有json文件中附加json对象 [英] Append json object in existing json file using Java

查看:188
本文介绍了使用Java在现有json文件中附加json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图每次在现有JSON数组中附加JSON对象.为此,我正在使用GSON库.尝试以下代码:

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();
}

但是它每次都创建JSON数组而不是附加.

But it created JSON array each time instead of appending.

实际:

[  
   {  
      "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"
   }
]

预期:

[  
   {  
      "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"
   }
]

任何帮助将不胜感激.

推荐答案

使用这种方法,您可能会拥有一个文件,其中包含许多数组,每个数组都有一个对象.我建议使用Gson.fromJson(..)& Gson.toJson(..)代替JsonWriter.

With your approach you might have a file that has many arrays each having one object. I suggest using Gson.fromJson(..) & Gson.toJson(..) instead of JsonWriter.

假设您要添加的对象如下:

Assume the object you want to add is like:

@AllArgsConstructor
@Getter @Setter
public class OrderIDDetailBean {
    private String uniqueID;
    private Integer orderID;
    private Date date;
    private Double cartTotal;        
}

然后附加新对象将如下:

then appending new object would go like:

@Test
public void test() throws Exception {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    // construct Type that tells Gson about the generic type
    Type dtoListType = new TypeToken<List<OrderIDDetailBean>>(){}.getType();
    FileReader fr = new FileReader("test.json");
    List<OrderIDDetailBean> dtos = gson.fromJson(fr, dtoListType);
    fr.close();
    // If it was an empty one create initial list
    if(null==dtos) {
        dtos = new ArrayList<>();
    }
    // Add new item to the list
    dtos.add(new OrderIDDetailBean("23", 34234, new Date(), 544.677));
    // No append replace the whole file
    FileWriter fw  = new FileWriter("test.json");
    gson.toJson(dtos, fw);
    fw.close();        
}

这篇关于使用Java在现有json文件中附加json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆