简体   繁体   中英

Add new key,value and update existing value of key in JSON file in java

My json file contains data as below: filename: "masterData.json"

{
  "Devices":[
    {"DeviceID":"WS1272765443SA",
      "DeviceModel":"model",
      "SerialNumber":"121",
      "Status":"available"
    },
    {
      "DeviceID":"WS1215",
      "DeviceModel":"eCP Tablet",
      "SerialNumber":"122",
      "Status":"available"

    }
  ],
  "Organization":[
    {
      "OrgName":"abc",
      "UserName":"hello",
      "OrgId":"abc",
      "CommunityURL":"",
      "ApiVersionValue":"1.0",
      "OrgCode":"Orgcode11",
      "OrgType":"en",
      "NameSpace":"h",
    }
  ],
  "DeviceModels":[
    {
      "BrandName":"",
      "ModelNameValue":"",
      "Config":""
    },
    {
      "BrandName":"",
      "ModelNameValue":"",
      "Config":""
    }
  ]
}

I need to add deviceNumber and value for each of my devices and also update the Status from available to active so that my "masterData.json" would look like

{
  "Devices":[
    {
      "DeviceID":"WS1272765443SA",
      "DeviceModel":"model",
      "SerialNumber":"121",
      "Status":**"active"**,
      **"deviceNumber":"123456D"**

    },
    {
      "DeviceID":"WS1215",
      "DeviceModel":"eCP Tablet",
      "SerialNumber":"122",
      "Status":**active**,
      **"deviceNumber":"1234f"**

    }
  ],
  "Organization":[
    {
      "OrgName":"abc",
      "UserName":"hello",
      "OrgId":"abc",
      "CommunityURL":"",
      "ApiVersionValue":"1.0",
      "OrgCode":"Orgcode11",
      "OrgType":"en",
      "NameSpace":"h",
    }
  ],
  "DeviceModels":[
    {
      "BrandName":"",
      "ModelNameValue":"",
      "Config":""
    },
    {
      "BrandName":"",
      "ModelNameValue":"",
      "Config":""
     }
  ]
}

Kindly help me with the java code.

i tried with below java code and couldn't achieve.

FileReader reader = new FileReader(utility.getValue("MasterJsonPath"));
JsonObject jsonObj = (JsonObject) parser.parse(reader);
JsonArray deviceListFromJSON = (JsonArray) jsonObj.get("Devices");

for (int i = 0; i < deviceListFromJSON.size(); i++) {

  JsonObject devices = (JsonObject) deviceListFromJSON.get(i);
   ObjectMapper mapper = new ObjectMapper();
   devices.addProperty("device number", "device value");
   deviceListFromJSON.add(devices);
  mapper.writer().writeValue(new File(utility.getValue("MasterJsonPath")),deviceListFromJSON);
}
}

First of, your fisrt json code is malformed ( extra commas, missing {"Devices", .. ).

I tried to correct it:

{"Devices": [
    {"DeviceID":"WS1272765443SA",
     "DeviceModel":"model",
     "SerialNumber":"121",
     "Status":"available"
    },
    {"DeviceID":"WS1215",
     "DeviceModel":"eCP Tablet",
     "SerialNumber":"122",
     "Status":"available"
 }],
"Organization": [
   {"OrgName":"abc",
    "UserName":"hello",
    "OrgId":"abc",
    "CommunityURL":"",
    "ApiVersionValue":"1.0",
    "OrgCode":"Orgcode11",
    "OrgType":"en",
    "NameSpace":"h"
}],
"DeviceModels":[
   {"BrandName":"",
    "ModelNameValue":"",
    "Config":""
   },
   {"BrandName":"",
    "ModelNameValue":"",
    "Config":""
}]}

For the java Code, try to save the file at the end of the for loop. And write the whole jsonobject " jsonObj ", not only the deviceListFromJSON . Also, use addProperty on both " deviceNumber " and " Status ".Hope it helps:)

    FileReader reader = new FileReader("file.json");
    JsonParser parser = new JsonParser() ;
    JsonObject jsonObj = (JsonObject) parser.parse(reader);
    JsonArray deviceListFromJSON = (JsonArray) jsonObj.get("Devices");
    reader.close();
    for (int i = 0; i < deviceListFromJSON.size(); i++) {
        JsonObject devices = (JsonObject) deviceListFromJSON.get(i);
        devices.addProperty("deviceNumber", "1234f");
        devices.addProperty("Status", "active");
    }   
    BufferedWriter writer = new BufferedWriter(new FileWriter("file.json"));
    writer.write(jsonObj.toString());
    writer.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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