比较两个 JSON 并显示 activity 我们更改了哪一列?

Compare two JSON and also show activity which column we changed?

我如何比较两个 JSON,一个是从数据库中获取的,另一个是从 UI 中获取的(意思是当我单击编辑按钮时)?我如何比较这两个 JSON?

第一个Json

 {
            "id": 2,
            "createdAt": "07-01-2021 15:26:16",
            "updatedAt": "07-01-2021 15:26:16",
            "offerAcceptedDate": "06-30-2021 07:14:00",
            "offerAmount": 100000,
            "offerDate": "06-30-2021 07:14:00",
            "offerFile": "string",
            "possibleCloseDate": "06-30-2021 07:14:00",
            "remarks": "string",
            "salesId": 8,
            "status": "Active",
            "contactId": 32,
            "createdById": 1,
            "offerAcceptedById": 1
        }

第 2 JSON

{
                
                "createdAt": "09-01-2021 15:26:16",
                "updatedAt": "10-01-2021 15:26:16",
                "offerAcceptedDate": "06-30-2021 07:14:00",
                "offerAmount": 500000,
                "offerDate": "06-30-2021 07:14:00",
                "offerFile": "string",
                "possibleCloseDate": "06-30-2021 07:14:00",
                "remarks": "string",
                "salesId": 8,
                "status": "Active",
                "contactId": 32,
                "createdById": 1,
                "offerAcceptedById": 1
            }

此外,比较这两个 JSON 并显示 activity 更改了哪些列。 像这样:

createdAt: Changed

updatedAt: Changed

offeramount: Changed

您可以为此使用 guava 库。例如:

  String json1 = "{\"name\":\"ABC\", \"city\":\"XYZ\", \"state\":\"CA\"}";
  String json2 = "{\"city\":\"XYZ\", \"street\":\"123 anyplace\", \"name\":\"ABC\"}";

  Gson g = new Gson();
  Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
  Map<String, Object> firstMap = g.fromJson(json1, mapType);
  Map<String, Object> secondMap = g.fromJson(json2, mapType);
  System.out.println(Maps.difference(firstMap, secondMap).entriesDiffering());

这显示了两个映射中都存在但具有不同值的键

1) 首先,我使用 findById(JPA Repository)

从数据库中获取数据

2)比较Db字段和请求体字段

像那样:

LeadOffer leadOffer = leadOfferRepository.findById(offerId).orElse(null);
        JsonObject jsonObject = new JsonObject();
                JsonArray jsonArray= new JsonArray();
            if (leadOffer.getAmount() != leadOfferRequest.getAmount())
                    {
                        offer.setAmount(leadOfferRequest.getAmount());
                        jsonObject.addProperty("amount",leadOfferRequest.getAmount() );
                        jsonObject1.addProperty("amount",leadOffer.getAmount() );
            
                    }
    jsonArray.add(jsonObject);