English 中文(简体)
发现JSON的无效财产和遗漏财产之间的差别
原标题:Detecting the difference between null property and omitted property in JSON

我有一个更新的网络服务 接收JSON, 它用数据合同JSONS序列器 将它分解为C#类, 尽管这可以改变。

其目的是更新资源字段

例如:

{
    "firstName" : "Martin"
}

我不一定想更新所有字段, 我希望找到一种方法, 检测JSON中哪些字段是、哪些字段不是未指明的。

但是,我无法找到这样做的方法,因为我不知道如何区分一个未指明的字段和一个应更新为无效的字段之间的差别。

例如:(不更新任何字段):

{}

vs:( 更新第一个命名字段为无效)

{
    "firstName" : null
}

解决这一问题的最佳办法是什么?

最佳回答

我想你可以用一个随机字符串初始化的字段

public class TestClass
{
    public string firstName = "some string";
}

如果您使用

PS:如果使用数据合同JsonServicer, 不要使用 Datacontract Data member 属性

问题回答

我不确定是谁在使用您的网络服务,但这听起来像是你应该决定的事情,然后告诉消费者如何操作(而不是依赖消费者来正确操作 ) 。 我不认为说所有领域都“ em> can 或“ em> 不能设置为无效” 是明智的, 因此对每个案例的处理都会有所不同 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签