您能否拥有一个包含破折号的属性名称

编程入门 行业动态 更新时间:2024-10-10 21:22:02
本文介绍了您能否拥有一个包含破折号的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以创建一个属性名称包含破折号的对象?

Is it possible to create an object with a property name that contains a dash character?

我正在创建一个匿名对象,以便可以使用Json.Net将其序列化为Json,并且我需要的属性之一包含一个'-'破折号.

I am creating an anonymous object so that I can serialize it to Json using Json.Net and one of the properties I need contains a '-' dash character.

我想要的一个例子是:

var document = { condtions = new { acl = "public-read", bucket = "s3-bucketname", starts-with = "test/path" } };

我知道我可以在创建对象时用下划线替换短划线,然后在序列化的字符串中替换短划线,但是我想知道如果没有这种解决方法,是否有一种语言可以做到这一点.

I know I could replace the dash with underscores when creating the object and then replace them in the serialized string afterwards, but wanted to know if there is a way in the language to do this without this workaround.

推荐答案

您不能对匿名对象执行此操作;字段名称必须是有效的标识符.您可以改用Dictionary,Json.Net应该使用Dictionary来像匿名对象一样轻松地进行序列化:

You can't do this with anonymous objects; field names must be valid identifiers. You could instead use a Dictionary, which Json.Net should serialise just as easily as an anonymous object:

var document = new { conditions = new Dictionary<string, string>() { { "acl", "public-read" }, { "bucket", "s3-bucketname" }, { "starts-with", "test/path" } } };

更多推荐

您能否拥有一个包含破折号的属性名称

本文发布于:2023-11-10 07:12:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:破折号   您能   属性   名称

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!