How to Update JSON value using Java

后端 未结 2 1487
無奈伤痛
無奈伤痛 2020-12-20 09:13

I have below json, i want to update each and every value of that json but sometimes only one value

{ 
"msgType": "NEW",
"code":          


        
2条回答
  •  情话喂你
    2020-12-20 10:06

    You need to write the updated JSON into the file from where JSON was read. Also, I did not understand your variable assignment so I have updated that as well. Use below code:

    FileReader reader = new FileReader(filePath);
    
    JSONParser jsonParser = new JSONParser();
    JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
    
    System.out.println(jsonObject);
    
    
    long id =Long.valueOf((String) idNewObj.get("plid"));
    System.out.println(id);
    
    
    jsonObject.put("plid",PL809809809);
    
    System.out.println(jsonObject);
    FileWriter writer = new FileWriter(filePath, false); //overwrites the content of file
    writer.write(jsonObject.toString());
    writer.close();
    

提交回复
热议问题