反序列化JSON对象的一部分,并且具有完整的剩余财产连载回 [英] Deserialize part of JSON object and serialize it back with the remaining properties intact

查看:284
本文介绍了反序列化JSON对象的一部分,并且具有完整的剩余财产连载回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些JSON,我想反序列化到一个C#类的一个实例。但是,类不具有的所有字段/属性配套原JSON。我希望能修改类的属性值,然后从原来的JSON仍然完好无损其余字段和属性序列化回JSON。

I have some JSON that I want to deserialize into an instance of a C# class. However, the class does not have all the fields/properties matching the original JSON. I would like to be able to modify the property values in the class and then serialize it back to JSON with the remaining fields and properties from the original JSON still intact.

有关举例来说,假设我有以下的JSON:

For example, let's say I have the following JSON:

{
    "name": "david",
    "age": 100,
    "sex": "M",
    "address": "far far away"
}

和我想将它反序列化到这个类:

And I want to deserialize it into this class:

class MyClass 
{  
    public string name { get; set; }  
    public int age { get; set; }  
}



反序列化后,我设置如下:

After deserializing, I set the following:

myClass.name = "John";
myClass.age = 200;

现在,我想序列化回JSON和得到这样的结果:

Now, I want to serialize it back to JSON and get this result:

{
    "name": "John",
    "age": 200,
    "sex": "M",
    "address": "far far away"
}

有没有办法做到这一点使用Json.Net?

Is there a way to do this using Json.Net?

推荐答案

您可以使用Json.Net的扩展数据功能来处理这

You can use Json.Net's "extension data" feature to handle this.

添加一个新的词典<字符串对象> 属性类,并用它标记 [JsonExtensionData] 属性。 (你可以把它的私人财产,如果你不想影响你的类的公共接口。)在反序列化,这本词典将不符合任何类的其他公共属性的任何数据来填补。在系列化,在字典中的数据将被写回的JSON作为对象的属性。

Add a new Dictionary<string, object> property to your class and mark it with a [JsonExtensionData] attribute. (You can make it a private property if you don't want to affect the public interface of your class.) On deserialization, this dictionary will be filled with any data that doesn't match any of the other public properties of your class. On serialization, the data in the dictionary will be written back out to the JSON as properties of the object.

class MyClass
{
    public string name { get; set; }
    public int age { get; set; }

    [JsonExtensionData]
    private Dictionary<string, object> otherStuff { get; set; }
}

下面是一个演示:

class Program
{
    static void Main(string[] args)
    {
        string json = @"
        {  
            ""name"": ""david"",
            ""age"": 100,
            ""sex"": ""M"",
            ""address"": ""far far away""
        }";

        MyClass obj = JsonConvert.DeserializeObject<MyClass>(json);

        Console.WriteLine("orig name: " + obj.name);
        Console.WriteLine("orig age: " + obj.age);
        Console.WriteLine();

        obj.name = "john";
        obj.age = 200;

        json = JsonConvert.SerializeObject(obj, Formatting.Indented);
        Console.WriteLine(json);
    }
}



输出:

Output:

orig name: david
orig age: 100

{
  "name": "john",
  "age": 200,
  "sex": "M",
  "address": "far far away"
}

这篇关于反序列化JSON对象的一部分,并且具有完整的剩余财产连载回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆