Вычислить различия между двумя объектами JSON в C# и верните полученный JSONC#

Место общения программистов C#
Ответить
Anonymous
 Вычислить различия между двумя объектами JSON в C# и верните полученный JSON

Сообщение Anonymous »

Допустим, у меня есть два объекта JSON ( и obj2 ), которые следуют той же схеме:

Код: Выделить всё

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
- это объект «более новой», поэтому цель состоит в том, что элементы в obj2 Обновление элементов в obj1 .

Подробнее здесь: https://stackoverflow.com/questions/765 ... -the-resul
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»