我有这样的杰森:
{
"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,然后执行更新。
您需要对代码进行小的更正。