使用JSON.NET填充不是JSON模式的一部分



我有一个看起来像这样的JSON对象:

{
    "Name": "John Smith"
    "Age": 18
    "Children" : [
        {
            "Name": "Little Johnny"
            "Age": 4
            "Children": []
        }
    ]
}

我的模型对象看起来像

public class Person {
    public string Name { get; set; }
    public int Age { get; set; }
    public IList<Person> Children { get; set; }
    public Guid? TrackingKey { get; set; }
}

您看到的" TrackingKey"属性不是JSON的一部分。

我想设置trackingkey属性,该属性具有我在JSON避难所中提供的值。

对父母和所有孩子的跟踪键都将相同。

但是,当我声明我的模型时,我可以使用defaultValue属性传递的静态值。

将跟踪键分配给父母及其所有孩子的最佳方法是什么?

您可以使用GUID来生成所需的值。

      string dados = Guid.NewGuid().ToString("N");

最新更新