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

In jsonpath how to put new value for nested json object?

我有 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" 下,我想 put/replace "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 put/replace 嵌套字段值?

由于 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
}

您的代码需要稍作更正。

JSONObject requestParams = Utilities.readJSON("createInvoiceFromQuote.json");
JSONArray linkedDocuments = requestParams.getJSONArray("linkedDocuments");
JSONObject document = linkedDocuments.getJSONObject(0) 
document.put("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
}