使用C#反序列化json-schema

时间:2016-06-06 18:31:14

标签: c# json json.net

我正在开发自己的JSON-to-POCO框架。 我这样做的方式是,我创建了一个类似于Json模式的类:

public class JsonSchema  
{
    public string type { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public Dictionary<string, JsonSchema> properties { get; set; }
    public JsonSchema items { get; set; }
    public string id { get; set; }
    public List<string> required { get; set; }
    public bool ispartial = true; 
}

并反序列化架构以获得我的对象,以便我可以完美地使用它。

现在一切正常,我正在获取生成的C#文件。但前提是我没有将$ref添加到我的json中。 (因为在json中添加引用的代码要少得多,而不是复制粘贴我想要支持它们的类)

我需要做的是,将$ref添加到我的班级。这里的问题是,我无法添加像

这样的属性
public string $ref { get; set; }

到我的代码。

知道我能做什么吗?

  

问题还在于,您无法使用默认设置反序列化$ id和$ ref。通过阅读这篇不错的帖子解决了这个问题:JsonConvert.DeserializeObject<> (string) returns null value for $id property

1 个答案:

答案 0 :(得分:2)

您可以将[JsonProperty]属性添加到要更改名称的属性中。

[JsonProperty("$ref")]
public string reference { get; set; }
相关问题