How to edit, modify nested JSONObject

前端 未结 4 1664
野趣味
野趣味 2021-02-14 21:29

Could you help me with this issue please. for example I have JSONEObject

{
\"glossary\": {
    \"title\": \"example glossary\",
    \"GlossDiv\": {
        \"ti         


        
4条回答
  •  长情又很酷
    2021-02-14 21:53

    Update/ edit/modify nested JSON Object and converting String to JSON by using org.json.simple.JSONObject recursive call

    JSON Input file

    {
      "Response": {
        "AccountId": "12345",
        "CompanyCode": 1,
        "CustomerName": "Joseph X. Schmoe",
        "EmailAddressList": {
          "Response.EmailAddressDTO": {
            "AlertOptionList": null,
            "ContactMethodSeqNum": 2,
            "EmailAddress": null
          }
        },
        "MailingAddress": {
          "NonStandard": null,
          "Standard": {
            "Address": "Example",
            "DisplayAddressText": null
          }
        },
        "LastBill": null,
        "LastPayment": null
      }
    }
    
    

    Code for the converting String to JSON Object and Updating the Nested JSON object value against the specific Key Example: "Address": "Addressxxxxxx",

    public static void main(String[] args) throws IOException {
    
            FileInputStream inFile = new FileInputStream("File_Location");
            byte[] str = new byte[inFile.available()];
            inFile.read(str);
            String string = new String(str);
            JSONObject json = JSONEdit.createJSONObject(string);
            System.out.println(JSONEdit.replacekeyInJSONObject(json,"Address","Addressxxxxxx"));
        }
    
        private static JSONObject replacekeyInJSONObject(JSONObject jsonObject, String jsonKey,
                String jsonValue) {
    
            for (Object key : jsonObject.keySet()) {
                if (key.equals(jsonKey) && ((jsonObject.get(key) instanceof String)||(jsonObject.get(key) instanceof Number)||jsonObject.get(key) ==null)) {
                    jsonObject.put(key, jsonValue);
                    return jsonObject;
                } else if (jsonObject.get(key) instanceof JSONObject) {
                    JSONObject modifiedJsonobject = (JSONObject) jsonObject.get(key);
                    if (modifiedJsonobject != null) {
                        replacekeyInJSONObject(modifiedJsonobject, jsonKey, jsonValue);
                    }
                }
    
            }
            return jsonObject;
        }
    
        private static JSONObject createJSONObject(String jsonString){
            JSONObject  jsonObject=new JSONObject();
            JSONParser jsonParser=new  JSONParser();
            if ((jsonString != null) && !(jsonString.isEmpty())) {
                try {
                    jsonObject=(JSONObject) jsonParser.parse(jsonString);
                } catch (org.json.simple.parser.ParseException e) {
                    e.printStackTrace();
                }
            }
            return jsonObject;
        }
    

    JSON Output:

    {
      "Response": {
        "AccountId": "12345",
        "CompanyCode": 1,
        "CustomerName": "Joseph X. Schmoe",
        "EmailAddressList": {
          "Response.EmailAddressDTO": {
            "AlertOptionList": null,
            "ContactMethodSeqNum": 2,
            "EmailAddress": null
          }
        },
        "MailingAddress": {
          "NonStandard": null,
          "Standard": {
            "Address": "Addressxxxxxx",
            "DisplayAddressText": null
          }
        },
        "LastBill": null,
        "LastPayment": null
      }
    }
    

提交回复
热议问题