简体   繁体   English

如何在java中更新现有的JSON文件

[英]how to update existing JSON file in java

This is my sample Json这是我的示例 Json

 {
        "State": {
            "version": "1",
            "SName": "Test",
            "shippingDetails": {
                "Address1": "AP",
                "ZipCode": "1236"
            },
            "Directions": {
                "routes": [
                    {
                        "taxAmount": "0.0",
                        "Quantity": "5",
                        "bounds": {
                            "SerialVersion": [
                                {
                                    "text": "1.7 km",
                                    "value": "1729",
                                    "time": "02633"
                                },
                                {
                                    "text": "1.9 km",
                                    "value": "1829",
                                    "time": "02353"
                                },
                                {
                                    "text": "17 km",
                                    "value": "1059",
                                    "time": "02133"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }

I want to update SName, ZipCode,taxAmount,Quantity, and text[1] values are there any way to do this.我想更新 SName、ZipCode、taxAmount、Quantity 和 text[1] 值有没有办法做到这一点。 I am taking JSON in a file and update tags are taking into HashMap我将 JSON 放入文件中,并且更新标签正在使用 HashMap

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. put(...)将用新值替换当前值。 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.并将其写回文件。

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

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