I want to deserialize a JSON string which does not necessarily contain data for every member, e.g:

public class MyStructure
{
   public string Field1;
   public string Field2;
}

Suppose I have an instance:

Field1: "data1"
Field2: "data2"

and I deserialize a string:

{ "Field1": "newdata1" }

结果应该是

Field1: "newdata1"
Field2: "data2"

Framework JavascriptSerializer and JSON.NET both return new objects in their deserialize methods, so the only way I can think of doing this directly would be to compare the deserialized object with the existing one using reflection which seems like a lot of unnecessary overhead. Ideally, some software would have a method in which I passed an existing instance of an object, and only those members which existed in the string would get updated. The point here is that I would like to be able to pass only data which has changed to the server, and update an existing object.

Is this possible using either of these tools, and if not, any suggestions on how to approach the problem?

推荐答案

在浏览了源代码之后(比阅读文档容易得多,是吗?)JSON.NET正是我想要的:

JsonConvert.PopulateObject(string, object)

Json.NET: Populate an Object

Json相关问答推荐

带有PowerShell内核的NewtonSoft json.Net的奇怪行为

Python将Pandas转换为嵌套的JSON

在AWS步骤函数中将字符串解析为JSON&S映射状态

如何将响应数据保存到Flutter 中的共享首选项中

JSONPath:查找子项目条件在字符串列表中的项目

JOLT 获取带有动态键的数组

在 JOLT 中重新排列值

如何在 Android Studio 中将 List 字段作为空列表[]返回?

如何限制resolveJsonModule的范围?

如何使用 ConfigurationBuilder 解析现有的 json 字符串(不是文件)

序列化特定类型时如何使 JSON.Net 序列化程序调用 ToString()?

如何使用 C# 将 JSON 文本转换为对象

Java JSON 序列化 - 最佳实践

IE中Json响应下载(7~10)

Spring MVC控制器中的JSON参数

使用 GSON 解析嵌套的 JSON 数据

使用 jq 从 bash 中管道分隔的键和值创建 JSON

如何在java中比较来自JsonObject的空值

将 CoffeeScript 项目转换为 JavaScript(不缩小)?

春天:返回@ResponseBodyResponseEntity>