Berechnen Sie die Unterschiede zwischen zwei JSON -Objekten in C# und geben Sie den resultierenden JSON zurückC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Berechnen Sie die Unterschiede zwischen zwei JSON -Objekten in C# und geben Sie den resultierenden JSON zurück

Post by Anonymous »

Nehmen wir an, ich habe zwei JSON -Objekte (

Code: Select all

obj1
und obj2 ), die demselben Schema folgen:

Code: Select all

obj1:
{
"id": "94774",
"name": "testName",
"colour": "red",
"date": {
"year": 2021,
"month": 10,
"day": 21
}
}
< /code>
obj2:
{
"id": "86970",
"name": "testName",
"foo": "bar",
"date": {
"year": 2022,
"month": 10,
"day": 21
}
}
< /code>
I would like to compute the difference between these objects, and have the resulting changes presented in the same schema as the original objects (comments are added for clarity here, and not required in the resulting output):
{
"id": "86970",  // modified
"colour": "",   // removed
"foo": "bar",   // added
"date": {
"year": 2022  // modified
}
}
< /code>
How would this be done? I have tried using JsonPatchDocument from Microsoft, and that can provide me with the differences between two objects, but I can't get it in the output format I'd like.
I am using C# but would consider any other relevant solutions to this problem.
Regarding Steve's comment:

How do you decide which of the two objects is the one to keep its values for building the difference json?

obj2
ist das "neuere" Objekt. Die Absicht ist daher, dass die Elemente in OBJ2 die Elemente in OBJ1 aktualisieren.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post