繁体   English   中英

将记录添加到java中的文件

[英]add record to a file in java

伙计们,我正在使用 jqgrid .. 这是我的文件

 { "rows":[ {"OrderID":"10248","FromDate":"1996-07-04","CustomerID":"WILMK","ShipName":"Vins et alcools Chevalier","ToDate":"1996-07-05"}, {"OrderID":"10249","FromDate":"1996-07-05","CustomerID":"TRADH","ShipName":"Toms Spezialit\äten","ToDate":"1996-07-17"}, {"OrderID":"10250","FromDate":"1996-07-08","CustomerID":"HANAR","ShipName":"Hanari Carnes","ToDate":"1996-07-26"}, {"OrderID":"10251","FromDate":"1996-07-08","CustomerID":"VICTE","ShipName":"Victuailles en stock","ToDate":"1996-08-01"}, {"OrderID":"10277","FromDate":"1996-08-09","CustomerID":"MORGK","ShipName":"Morgenstern Gesundkost","ToDate":"1996-08-12"} ] }

但我希望它像

 { "rows":[ {"OrderID":"10248","FromDate":"1996-07-04","CustomerID":"WILMK","ShipName":"Vins et alcools Chevalier","ToDate":"1996-07-05"}, {"OrderID":"10249","FromDate":"1996-07-05","CustomerID":"TRADH","ShipName":"Toms Spezialit\äten","ToDate":"1996-07-17"}, {"OrderID":"10250","FromDate":"1996-07-08","CustomerID":"HANAR","ShipName":"Hanari Carnes","ToDate":"1996-07-26"}, {"OrderID":"10251","FromDate":"1996-07-08","CustomerID":"VICTE","ShipName":"Victuailles en stock","ToDate":"1996-08-01"}, {"OrderID":"10277","FromDate":"1996-08-09","CustomerID":"MORGK","ShipName":"Morgenstern Gesundkost","ToDate":"1996-08-12"}, {"OrderID":"10261","FromDate":"1996-07-19","CustomerID":"QUEDE","ShipName":"Que Del\ícia","ToDate":"1996-08-09"} // added row ] }

从您的问题看来,您想在数组中的 json 文件中添加另一个对象。 所以,这是一个解决方案。

  • 使用您用来从文件读取/写入 json 的任何库读取您的 json 文件。
  • 在特定列表中添加新对象。
  • 使用覆盖模式将其写入文件。

首先解析你的 JSON 内容:

String input = "{\"rows\":[{\"OrderID\":\"10248\"}]}";
JSONObject json = new JSONObject(input);

然后从解析的内容中获取数组:

JSONArray arr = json.getJSONArray("rows");

转换对象以添加到 JSON:

JSONObject newRow  = new JSONObject();
newRow.accumulate("OrderID", 10000);
// Do the same for all your attributes

最后将新行添加到数组以获取更新的 JSON:

arr.put(newRow);
String updatedJSON = json.toString();

您想在数组中的json文件中添加另一个对象。 所以,这是一个解决方案。

  1. 阅读您的json文件。

  2. 将您的关键row作为JSONArray并将其存储到一个字符串中。

  3. 构建一个类Order

     Class Order { String OrderID ; String FromDate ; String CustomerID String ShipName; String ToDate; }

在特定列表中添加新对象。

//import Class (add a jar com.fasterxml.jackson to your project)
import com.fasterxml.jackson.databind.ObjectMapper;


// Here I assume that "applicationArray" contains the Array .
 String applicationArray = "[
        {"OrderID":"10248","FromDate":"1996-07-04","CustomerID":"WILMK","ShipName":"Vins et alcools Chevalier","ToDate":"1996-07-05"},
        {"OrderID":"10249","FromDate":"1996-07-05","CustomerID":"TRADH","ShipName":"Toms Spezialit\u00e4ten","ToDate":"1996-07-17"},
        {"OrderID":"10250","FromDate":"1996-07-08","CustomerID":"HANAR","ShipName":"Hanari Carnes","ToDate":"1996-07-26"},
        {"OrderID":"10251","FromDate":"1996-07-08","CustomerID":"VICTE","ShipName":"Victuailles en stock","ToDate":"1996-08-01"},
        {"OrderID":"10277","FromDate":"1996-08-09","CustomerID":"MORGK","ShipName":"Morgenstern Gesundkost","ToDate":"1996-08-12"},
        {"OrderID":"10261","FromDate":"1996-07-19","CustomerID":"QUEDE","ShipName":"Que Del\u00edcia","ToDate":"1996-08-09"} // added row
    ]"

ObjectMapper mapper = new ObjectMapper();
List<Order> obj_listOrder = mapper.readValue(applicationArray, mapper.getTypeFactory().constructCollectionType(List.class, DeviceApplication.class));

现在再向列表中添加一个元素。

再向列表中添加一个元素。 替换您的文件的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM