英文:
Unable to change the attribute value present in Json using JSONObject
问题
我需要您的帮助,因为我的要求是获取位于JSON路径Request.eventDetails.message
部分下的属性之一的值,该属性名为transactionReferenceNumber
。
当然,我对JSON路径遍历的了解有限,但我已经能够使用以下代码片段中的JSONObject
遍历到Request.eventDetails.message
,但无法遍历到message
部分并获取或更改transactionReferenceNumber
属性或任何其他属性的值。
String filepath = System.getProperty("user.dir") +
"/src/test/resources/Json_Files/Payment_Events_Api_PayLoad.json";
String jsonContents = new String((Files.readAllBytes(Paths.get(filepath))));
JSONObject jsonObject = new JSONObject(jsonContents);
String Message = jsonObject.getJSONObject("request")
.getJSONObject("eventDetails")
.get("message")
.toString();
以下是供您参考的JSON:
{
"metadata": {
"requestId": "e1d0db1f-ac7d-4011-b34e-c063b6d638a6",
"signature": "ZwcQa6YozllV1mLWPrZFiacSguRoEibSgmb97UvTrWgvuY5I76eWuAg==",
"signatureAlgorithm": "SH443222wesd"
},
"request": {
"eventId": "e43e2222-2c91-47d8-8c03-9a5f52db15a2",
"eventCode": "EADFE",
"eventCategory": "PAYMENT_STATUS",
"eventType": "TRANSACTION_STATUS",
"eventDetails": {
"paymentProduct": "CXC",
"message": "{ \"achCompanyId\": \"3131231\", \"companyName\": \"ABC Company\", \"transactionReferenceNumber\": \"4333\", \"remittanceInfo\": \"Paroll Processing\", \"processingDate\": \"2022-01-12\", \"payeeFirstname\": \"John\", \"payeeIDType\": \"email\", \"payeeEmail\": \"abc.Doe@gmail.com\", \"payeeMobile\": \"\", \"paymentType\": \"STANDARD\", \"paymentAmount($)\": 600.50, \"numberOfHoldDays\": 5, \"currentPaymentStatus\": \"PENDING\"}",
"messageFormat": "CUSTOM.CXC.JSON"
},
"timeStamp": "2020-07-21T14:53:54-0400"
}
}
请您帮忙解决这个问题,并建议可能的解决方法。提前感谢。
英文:
I need your help here as my requirement is to have the value for one of the attributes called transactionReferenceNumber
which comes under JSON path Request.eventDetails.message
section.
Of course, with little knowledge on the JSON path traversing I was able to traverse till Request.eventDetails.message
using JSONObject
as shown in below snippet code, but unable to traverse in message
section and get or change the value for transactionReferenceNumber
attribute or any other attribute.
String filepath = System.getProperty("user.dir") +
"/src/test/resources/Json_Files/Payment_Events_Api_PayLoad.json";
String jsonContents = new String((Files.readAllBytes(Paths.get(filepath))));
JSONObject jsonObject = new JSONObject(jsonContents);
String Message = jsonObject.getJSONObject("request")
.getJSONObject("eventDetails")
.get("message")
.toString();
Below is the JSON for your reference:
{
"metadata": {
"requestId": "e1d0db1f-ac7d-4011-b34e-c063b6d638a6",
"signature": "ZwcQa6YozllV1mLWPrZFiacSguRoEibSgmb97UvTrWgvuY5I76eWuAg==",
"signatureAlgorithm": "SH443222wesd"
},
"request": {
"eventId": "e43e2222-2c91-47d8-8c03-9a5f52db15a2",
"eventCode": "EADFE",
"eventCategory": "PAYMENT_STATUS",
"eventType": "TRANSACTION_STATUS",
"eventDetails": {
"paymentProduct": "CXC",
"message": "{ \"achCompanyId\": \"3131231\", \"companyName\": \"ABC Company\", \"transactionReferenceNumber\": \"4333\", \"remittanceInfo\": \"Paroll Processing\", \"processingDate\": \"2022-01-12\", \"payeeFirstname\": \"John\", \"payeeIDType\": \"email\", \"payeeEmail\": \"abc.Doe@gmail.com\", \"payeeMobile\": \"\", \"paymentType\": \"STANDARD\", \"paymentAmount($)\": 600.50, \"numberOfHoldDays\": 5, \"currentPaymentStatus\": \"PENDING\"}",
"messageFormat": "CUSTOM.CXC.JSON"
},
"timeStamp": "2020-07-21T14:53:54-0400"
}
}
Please, request in all to help me on this issue and also suggest the possible way to tackle this problem.
Thanks in advance.
答案1
得分: 0
"The "message" value in your JSON is a string containing another JSON object, not a direct JSON object, meaning that you cannot traverse it directly using getJSONObject()
or similar methods. What you need to do is to parse the "message" as a separate JSON object, and only then you can modify its values:
String filepath = System.getProperty("user.dir") +
"/src/test/resources/Json_Files/Payment_Events_Api_PayLoad.json";
String jsonContents = new String((Files.readAllBytes(Paths.get(filepath))));
JSONObject jsonObject = new JSONObject(jsonContents);
String messageStr = jsonObject.getJSONObject("request")
.getJSONObject("eventDetails")
.getString("message");
// get message separately
JSONObject messageJson = new JSONObject(messageStr);
// modify your value
messageJson.put("transactionReferenceNumber", "NewValue");
// replace the old message string with the modified one
jsonObject.getJSONObject("request")
.getJSONObject("eventDetails")
.put("message", messageJson.toString());
英文:
The "message" value in your JSON is a string containing another JSON object, not a direct JSON object, meaning that you cannot traverse it directly using getJSONObject()
or similar methods. What you need to do is to parse the "message" as a separate JSON object, and only then you can modify its values:
String filepath = System.getProperty("user.dir") +
"/src/test/resources/Json_Files/Payment_Events_Api_PayLoad.json";
String jsonContents = new String((Files.readAllBytes(Paths.get(filepath))));
JSONObject jsonObject = new JSONObject(jsonContents);
String messageStr = jsonObject.getJSONObject("request")
.getJSONObject("eventDetails")
.getString("message");
// get message separately
JSONObject messageJson = new JSONObject(messageStr);
// modify your value
messageJson.put("transactionReferenceNumber", "NewValue");
// replace the old message string with the modified one
jsonObject.getJSONObject("request")
.getJSONObject("eventDetails")
.put("message", messageJson.toString());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论