使用Ref属性

时间:2015-05-19 10:19:43

标签: c# json github json.net

我试图反序列化从GitHubs webhook for a push event发布的JSON。

它使用" ref"存储分支信息的属性,但ref是C#中的保留字,因此序列化不起作用。

现在我有

public class PushEvent
{
    [JsonProperty("ref")]
    public string _ref { get; set; }
    public string before { get; set; }
    public string after { get; set; }
    public bool created { get; set; }
    public bool deleted { get; set; }
    public bool forced { get; set; }
    public object base_ref { get; set; }
    public string compare { get; set; }
    public Commit[] commits { get; set; }
    public Head_Commit head_commit { get; set; }
    public Repository repository { get; set; }
    public Pusher pusher { get; set; }
    public Sender sender { get; set; }
}

但_ref始终设置为null。作为参考,这里也是将推送数据写入文件的MVC操作 - 只是在有所作为的情况下

[HttpPost]
public JsonResult PushEvent(PushEvent data)
{
    var dataString = JsonConvert.SerializeObject(data);
    using(var writer = System.IO.File.CreateText(Server.MapPath("/app_data/" + DateTime.UtcNow.ToString("yyyyMMddhhmmss") + ".json")))
    {
        writer.Write(dataString);
    }
    return new JsonResult(){Data="ok"};
}

1 个答案:

答案 0 :(得分:2)

尝试将其更改为:

public class PushEvent
{
    public string @ref { get; set; }