如何在Java中更新现有的JSON文件 [英] how to update existing JSON file in java

查看:235
本文介绍了如何在Java中更新现有的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"
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }

我想更新SName,ZipCode,taxAmount,Quantity和text [1]值 有什么办法可以做到这一点.我正在文件中使用JSON,而更新标签已纳入HashMap

I want to update SName, ZipCode,taxAmount,Quantity, and text[1] values are there any way to do this. I am taking JSON in a file and update tags are taking into HashMap

推荐答案

JSONObject jsonObject = new JSONObject("Your_JSON_String");

JSONObject jsonObjectForState = jsonObject.getJSONObject("State");

jsonObjectForState.put("Sname", "New_Value_Here");

put(...)将用新值替换当前值.同样,您可以更新其他值.完成后,您可以使用以下方法将其转换回去:

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();

并将其写回到文件中.

这篇关于如何在Java中更新现有的JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆