简体   繁体   中英

java - replace string in JSON with json array of objects

I've a JSON:

    {
    "payment_intent": {
        "amount": "Amount",
        "currency_code": "840",
        "invoice_number": "pay-automation-invoice-no-01",
        "payment_intent_id": "pay-automation-return-intent-id-01",
        "intent_reference_id": "pay-automation-intent-reference-id-01"
    },
    "payment_refundable_intents": {
        "transactions": {
            "sales": "pay-automation-sales"
        }
    }
}

Now, when I tried to replace string "pay-automation-sales" with JSONArray using

payloadJson = payloadJson.replaceAll("pay-automation-sales", salesString);

salesString is

[{"amount":"200.0","payment_intent_id":"29518150","tender_type":"cash","reference_id":"db79893a-9fe0-4391-91f8-fbc-cash-6c88-66db","intent_reference_id":"db79893a-9fe0-4391-91f8-fbc7945ce446","id":"000000893275","status":"Approved"},{"amount":"800.0","payment_intent_id":"29518150","tender_type":"cash","reference_id":"db79893a-9fe0-4391-91f8-fbc-cash-1d12-8466","intent_reference_id":"db79893a-9fe0-4391-91f8-fbc7945ce446","id":"000000893282","status":"Approved"}]

Here, payloadJson is of type String . The replaceAll works fine but actually I want to pass "sales" as an array of object in JSON. But it is getting passed like this and it's not a valid JSON format. Double quotes in value of sales key in JSON causes an issue I think.

"sales": "[{"amount":"200.0","payment_intent_id":"29518150","tender_type":"cash","reference_id":"db79893a-9fe0-4391-91f8-fbc-cash-6c88-66db","intent_reference_id":"db79893a-9fe0-4391-91f8-fbc7945ce446","id":"000000893275","status":"Approved"},{"amount":"800.0","payment_intent_id":"29518150","tender_type":"cash","reference_id":"db79893a-9fe0-4391-91f8-fbc-cash-1d12-8466","intent_reference_id":"db79893a-9fe0-4391-91f8-fbc7945ce446","id":"000000893282","status":"Approved"}]"

How do I replace string in JSON with valid JSON array of objects?

Since you're working with String objects here and not some form of JSON object model, when you did

payloadJson = payloadJson.replaceAll("pay-automation-sales", salesString);

it found the string

pay-automation-sales

within payloadJson and replaced it verbatin with the contents of salesString . Notice that you did NOT tell it to include the quotes in the original string in the part being replaced.

It should be

payloadJson = payloadJson.replaceAll("\"pay-automation-sales\"", salesString);

You would probably be better off using a real JSON library that understands JSON syntax and can manipulate the JSON as an in-memory document model.

Using Java String

public class StringDemo {
    static final String originalJson = "    {\n"
            + "    \"payment_intent\": {\n"
            + "        \"amount\": \"Amount\",\n"
            + "        \"currency_code\": \"840\",\n"
            + "        \"invoice_number\": \"pay-automation-invoice-no-01\",\n"
            + "        \"payment_intent_id\": \"pay-automation-return-intent-id-01\",\n"
            + "        \"intent_reference_id\": \"pay-automation-intent-reference-id-01\"\n"
            + "    },\n"
            + "    \"payment_refundable_intents\": {\n"
            + "        \"transactions\": {\n"
            + "            \"sales\": \"pay-automation-sales\"\n"
            + "        }\n"
            + "    }\n"
            + "}";
    
    static final String originalArray = "[{\"amount\":\"200.0\",\"payment_intent_id\":\"29518150\",\"tender_type\":\"cash\",\"reference_id\":\"db79893a-9fe0-4391-91f8-fbc-cash-6c88-66db\",\"intent_reference_id\":\"db79893a-9fe0-4391-91f8-fbc7945ce446\",\"id\":\"000000893275\",\"status\":\"Approved\"},{\"amount\":\"800.0\",\"payment_intent_id\":\"29518150\",\"tender_type\":\"cash\",\"reference_id\":\"db79893a-9fe0-4391-91f8-fbc-cash-1d12-8466\",\"intent_reference_id\":\"db79893a-9fe0-4391-91f8-fbc7945ce446\",\"id\":\"000000893282\",\"status\":\"Approved\"}]";

    public static void main(String[] args) {
        String editedJson = originalJson.replaceAll("\"pay-automation-sales\"", originalArray);
        System.out.println(editedJson);;
    }
}

Using Jackson

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.*;

public class JacksonDemo {
    static final String originalJson = "    {\n"
            + "    \"payment_intent\": {\n"
            + "        \"amount\": \"Amount\",\n"
            + "        \"currency_code\": \"840\",\n"
            + "        \"invoice_number\": \"pay-automation-invoice-no-01\",\n"
            + "        \"payment_intent_id\": \"pay-automation-return-intent-id-01\",\n"
            + "        \"intent_reference_id\": \"pay-automation-intent-reference-id-01\"\n"
            + "    },\n"
            + "    \"payment_refundable_intents\": {\n"
            + "        \"transactions\": {\n"
            + "            \"sales\": \"pay-automation-sales\"\n"
            + "        }\n"
            + "    }\n"
            + "}";
    
    static final String originalArray = "[{\"amount\":\"200.0\",\"payment_intent_id\":\"29518150\",\"tender_type\":\"cash\",\"reference_id\":\"db79893a-9fe0-4391-91f8-fbc-cash-6c88-66db\",\"intent_reference_id\":\"db79893a-9fe0-4391-91f8-fbc7945ce446\",\"id\":\"000000893275\",\"status\":\"Approved\"},{\"amount\":\"800.0\",\"payment_intent_id\":\"29518150\",\"tender_type\":\"cash\",\"reference_id\":\"db79893a-9fe0-4391-91f8-fbc-cash-1d12-8466\",\"intent_reference_id\":\"db79893a-9fe0-4391-91f8-fbc7945ce446\",\"id\":\"000000893282\",\"status\":\"Approved\"}]";

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = mapper.readTree(originalJson);
        
        JsonNode array = mapper.readTree(originalArray);
        
        ObjectNode transactions = (ObjectNode) root.findValue("transactions");
        transactions.set("sales", array);
        
        String editedJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
        System.out.println(editedJson);;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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