简体   繁体   English

使用现有项将json反序列化为C#列表

[英]Deserializing json to C# list with existing items

Given the following classes: 鉴于以下类别:

class Report {

    public Report() {
        this.Fields=new List<Field>();
    }

    [JsonProperty("fields")]
    public IList<Field> Fields { get; private set; }
}

class Field {

    [JsonProperty("identifier")]
    public Guid Identfier { get;set; }

    [JsonProperty("name")]
    public string Name { get;set; }
}

and the following test method set up: 并设置以下测试方法:

var report = new Report();
report.Fields.Add(new Field { Identifier = new Guid("26a94eab-3d50-4330-8203-e7750abaa060"), Name = "Field 1" });
report.Fields.Add(new Field { Identifier = new Guid("852107db-b5d1-4344-9f71-7bd90b96fec0"), Name = "Field 2" });

var json = "{\"fields\":[{\"identifier\":\"852107db-b5d1-4344-9f71-7bd90b96fec0\",\"name\":\"name changed\"},{\"identifier\":\"ac424aff-22b5-4bf3-8232-031eb060f7c2\",\"name\":\"new field\"}]}";

JsonConvert.PopulateObject(json, report);

Assert.IsTrue(report.Fields.Count == 2, "The number of fields was incorrect.");

How do I get JSON.Net to know that the field with identifier "852107db-b5d1-4344-9f71-7bd90b96fec0" should apply to the existing field with the same identifier? 如何让JSON.Net知道标识符为“852107db-b5d1-4344-9f71-7bd90b96fec0”的字段应该应用于具有相同标识符的现有字段?

Also, is it possible to get JSON.Net to remove items that do not exist within the given JSON array, (specifically the field with identifier "26a94eab-3d50-4330-8203-e7750abaa060" should be removed because it does not exist in the given json array. 此外,是否可以让JSON.Net删除给定JSON数组中不存在的项目(特别是应删除标识符为“26a94eab-3d50-4330-8203-e7750abaa060”的字段,因为它不存在于给出了json数组。

If there is a way to manually code or override the way that JSON analyses a list then that would be better because I could write the code to say "this is the item you need" or "use this newly created item" or just "don't do anything to this item because I have removed it". 如果有一种方法可以手动编码或覆盖JSON分析列表的方式那么那会更好,因为我可以编写代码来说“这是你需要的项目”或“使用这个新创建的项目”或者只是“不要”我对这个项目做了什么,因为我已经删除了它“。 Anyone know of a way I can do this please? 有人知道我可以这样做吗?

You can use the option ObjectCreationHandling = ObjectCreationHandling.Replace . 您可以使用选项ObjectCreationHandling = ObjectCreationHandling.Replace

You can do this for your entire data model using serializer settings, as is shown in Json.Net PopulateObject Appending list rather than setting value : 您可以使用序列化程序设置为整个数据模型执行此操作,如Json.Net PopulateObject附加列表中所示, 而不是设置值

    var serializerSettings = new JsonSerializerSettings {ObjectCreationHandling = ObjectCreationHandling.Replace};
    JsonConvert.PopulateObject(json, report, serializerSettings);

Or, you can set the option on the JsonProperty attribute you are already using if you don't want to do this universally: 或者,如果您不想普遍这样做,可以在已经使用的JsonProperty属性上设置选项:

class Report
{
    public Report()
    {
        this.Fields = new List<Field>();
    }

    [JsonProperty("fields", ObjectCreationHandling = ObjectCreationHandling.Replace)]
    public IList<Field> Fields { get; private set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM