how to update existing JSON file in java

前端 未结 1 626
说谎
说谎 2020-12-12 07:35

This is my sample Json

 {
        \"State\": {
            \"version\": \"1\",
            \"SName\": \"Test\",
            \"shippingDetails\": {
                   


        
相关标签:
1条回答
  • 2020-12-12 08:13
    JSONObject jsonObject = new JSONObject("Your_JSON_String");
    
    JSONObject jsonObjectForState = jsonObject.getJSONObject(“State”);
    
    jsonObjectForState.put("Sname", "New_Value_Here");
    

    put(...) will replace the current value with new value. Similarly, you can update the other values. Once you are done, you can convert it back using:

    jsonObject.toString();
    

    And write it back to the file.

    0 讨论(0)
提交回复
热议问题