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

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

问题描述

随着J​​SON定义,因为它是为了反序列化作为一个对象,我需要创建我的课称为事件,这是一个C#的关键字属性。 ?是否有另一种方式告诉它的字段名称将是什么



下面是JSON的例子:



<块引用>

{事件:123数据:
{数据:0D0401,TTL:60,published_at:2014-04-16T18:04:42.446Z ,ID:48ff6f065067555031192387}
}




下面是我的课因为这将无法编译关键字:

 公共类的事件
{
公众诠释事件{搞定;组; }
公共EventDetail数据{搞定;组; }
}

公共类EventDetail
{
公共字符串数据{搞定;组; }
公共字符串TTL {搞定;组; }
公众的DateTime published_at {搞定;组; }
公共字符串ID {搞定;组; }
}


解决方案

尝试使用 [DataContract(NAME =@event)] 对有关财产的属性。然后,它会(德)正确序列化,所以它编译您可以重命名属性。


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?

Here's an example of the JSON:

{ 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; }
}

解决方案

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天全站免登陆