要反序列化的对象有一个 C# 关键字 [英] object to deserialize has a C# keyword

查看:20
本文介绍了要反序列化的对象有一个 C# 关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON 已按原样定义,为了将其反序列化为对象,我需要在我的类上创建一个名为event"的属性,这是一个 C# 关键字.有没有其他方法可以告诉它字段名称是什么?

With the JSON defined as it is, in order to deserialize it as an object, I'd need to create a property on my class called "event", which is a C# keyword. Is there another way to tell it what the field names will be?

以下是 JSON 的示例:

Here's an example of the JSON:

{ 事件:123 数据:{"data":"0D0401","ttl":"60","published_at":"2014-04-16T18:04:42.446Z","id":"48ff6f065067555031192387"}}

{ event: 123 data: {"data":"0D0401","ttl":"60","published_at":"2014-04-16T18:04:42.446Z","id":"48ff6f065067555031192387"} }

以下是由于关键字而无法编译的类:

Here are my classes that won't compile because of the keyword:

public class Event
{
    public int event { get; set; }
    public EventDetail data { get; set; }
}

public class EventDetail
{
    public string data { get; set; }
    public string ttl { get; set; }
    public DateTime published_at { get; set; }
    public string id { get; set; }
}

推荐答案

尝试使用相关属性上的 [DataContract(Name = "@event")] 属性.然后它将正确(反)序列化,您可以重命名属性以便编译.

Try using the [DataContract(Name = "@event")] attribute on the relevant property. Then it will (de)serialize correctly, and you can rename the property so that it compiles.

这篇关于要反序列化的对象有一个 C# 关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆