gpt4 book ai didi

java - 在 jsonpath 中如何为嵌套 json 对象添加新值?

转载 作者:行者123 更新时间:2023-12-01 19:30:18 25 4
gpt4 key购买 nike

我有这样的 Json:

{
"attachments": [
"string"
],
"contact": {
"name": "Mahesh"
},
"contactCode": "C-0000001",
"journalEntryCode": "JE-0000002",
"linkedDocuments": [
{
"contactCode": "C-0000001",
"documentCode": "0000018",
"documentItemDetails": [
{
"productCode": "P-0000001"
}
],
"documentType": "QUOTATION",
}
],
"taxAmount": 2.322,
"totalAmount": 12.322,
"unitPriceGstInclusive": false
}

在“linkedDocuments”下,我想为“documentCode”放置/替换新值“1234”我尝试通过采用 json 路径来执行以下操作:

JSONObject requestParams = Utilities.readJSON("createInvoiceFromQuote.json");

requestParams.put("$.linkedDocuments[*].documentCode", "1234");

但它只是在 json 末尾创建一个新字段“$.linkedDocuments[*].documentCode”,如下所示

{
"attachments": [
"string"
],
"contact": {
"name": "Mahesh"
},
"contactCode": "C-0000001",
"journalEntryCode": "JE-0000002",
"linkedDocuments": [
{
"contactCode": "C-0000001",
"documentCode": "0000018",
"documentItemDetails": [
{
"productCode": "P-0000001"
}
],
"documentType": "QUOTATION",
}
],
"taxAmount": 2.322,
"totalAmount": 12.322,
"unitPriceGstInclusive": false
"$.linkedDocuments[*].documentCode":"1234"
}

应该是这样的

{
"attachments": [
"string"
],
"contact": {
"name": "Mahesh"
},
"contactCode": "C-0000001",
"journalEntryCode": "JE-0000002",
"linkedDocuments": [
{
"contactCode": "C-0000001",
"documentCode": "1234",
"documentItemDetails": [
{
"productCode": "P-0000001"
}
],
"documentType": "QUOTATION",
}
],
"taxAmount": 2.322,
"totalAmount": 12.322,
"unitPriceGstInclusive": false
}

如何使用 java 放置/替换嵌套字段值?

最佳答案

由于 linkedDocuments 是 JSONObject 的 JSONArray,因此“$.linkedDocuments[*].documentCode”键将无法按预期工作。要更新 JSONArray 中 JSONObject 的任意键,请访问 JOSNArray 并迭代所有 JSONObject,然后执行更新。

String str = "{\"attachments\":[\"string\"],\"contact\":{\"name\":\"Mahesh\"},\"contactCode\":\"C-0000001\",\"journalEntryCode\":\"JE-0000002\",\"linkedDocuments\":[{\"contactCode\":\"C-0000001\",\"documentCode\":\"0000018\",\"documentItemDetails\":[{\"productCode\":\"P-0000001\"}],\"documentType\":\"QUOTATION\"},{\"contactCode\":\"C-0000002\",\"documentCode\":\"0000019\",\"documentItemDetails\":[{\"productCode\":\"P-0000002\"}],\"documentType\":\"QUOTATION\"}],\"taxAmount\":2.322,\"totalAmount\":12.322,\"unitPriceGstInclusive\":false}";

JSONObject jsonObject = new JSONObject(str);

JSONArray array = jsonObject.getJSONArray("linkedDocuments");
array.getJSONObject(0).put("documentCode", 1234);
System.out.println(jsonObject);

输出:

{
"linkedDocuments": [
{
"documentCode": 1234,
"contactCode": "C-0000001",
"documentType": "QUOTATION",
"documentItemDetails": [
{
"productCode": "P-0000001"
}
]
},
{
"documentCode": "0000019",
"contactCode": "C-0000002",
"documentType": "QUOTATION",
"documentItemDetails": [
{
"productCode": "P-0000002"
}
]
}
],
"totalAmount": 12.322,
"attachments": [
"string"
],
"contactCode": "C-0000001",
"contact": {
"name": "Mahesh"
},
"journalEntryCode": "JE-0000002",
"taxAmount": 2.322,
"unitPriceGstInclusive": false
}

关于java - 在 jsonpath 中如何为嵌套 json 对象添加新值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59942831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com