JavaGSON更新json文件
我希望能够从现有的两个json文件中创建一个新的json文件
json文件的第一个列表如下:
{
"Jar_serviceid": "service_v1",
"Jar_version": "1.0",
"ServiceId": "srv_v1",
"ServiceState": "Enable",
"ServiceVersion": "v1",
"LicenseRequired": false,
"ServiceURL": null
}
第二个是:
{
"Include":[
{
"Jar_serviceid":"service_v1",
"Jar_version":"1.0",
"ServiceState":"null"
}
],
"Exclude":"rtm_v2"
}
读出这两个文件后,我希望第二个文件更新第一个文件。在这种情况下,我希望在最后有这样的东西:
{
"Jar_serviceid": "service_v1",
"Jar_version": "1.0",
"ServiceId": "srv_v1",
"ServiceState": "null",
"ServiceVersion": "v1",
"LicenseRequired": false,
"ServiceURL": null
}
所以第二个json文件中的每个条目,同时编辑第一个。你有我的入口吗
我试过这样的方法:
if (secondconfig != null) {
if (secondconfig .getInclude() != null) {
for (ServiceList service : secondconfig.getInclude()) {
for (int i = 0; i < firstconfig.length; i++) {
ServiceList serv = gson.fromJson(firstconfig[i], ServiceList.class);
if(serv.getServiceId().equalsIgnoreCase(service.getJar_serviceid())){
// update
}
}
}
}
if (updatedconfig.getExclude() != null) {
System.out.println("Execlude: " + updatedconfig.getExclude());
}
if (updatedconfig.getVin() != null) {
System.out.println("VIN: " + updatedconfig.getVin());
}
}
也许有更好的方法?谢谢
# 1 楼答案
我们需要通过第二个json的值进行迭代,并对原始数据进行必要的更新,我会这样做:
根据我们接收的数据的性质/格式,我们可能需要添加额外的空/类型检查