使用 Ref 属性 反序列化一个 JSON 对象
Deserialize a JSON object with Ref property
我正在尝试反序列化从 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 始终设置为空。作为参考,这里还有将推送的数据写入文件的 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"};
}
尝试将其更改为:
public class PushEvent
{
public string @ref { get; set; }
我正在尝试反序列化从 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 始终设置为空。作为参考,这里还有将推送的数据写入文件的 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"};
}
尝试将其更改为:
public class PushEvent
{
public string @ref { get; set; }