4

最近我在类中遇到了一个 json 对象名称声明,并且能够通过以下方式解决它:

  [JsonProperty("flv-270p")]
        public string flv270p { get; set; }

那么如果我想在没有 JsonProperty 的情况下声明 flv-270p 我该怎么做呢?

喜欢 :

public string flv270-p { get; set; }
4

5 回答 5

6

你不能:

有效名称定义为:

identifier:
    available-identifier
    @   identifier-or-keyword
available-identifier:
    An identifier-or-keyword that is not a keyword
identifier-or-keyword:
    identifier-start-character   identifier-part-charactersopt
identifier-start-character:
    letter-character
    _ (the underscore character U+005F) 
identifier-part-characters:
    identifier-part-character
    identifier-part-characters   identifier-part-character
identifier-part-character:
    letter-character
    decimal-digit-character
    connecting-character
    combining-character
    formatting-character
letter-character:
    A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl
    A unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl
combining-character:
    A Unicode character of classes Mn or Mc
    A unicode-escape-sequence representing a character of classes Mn or Mc
decimal-digit-character:
    A Unicode character of the class Nd
    A unicode-escape-sequence representing a character of the class Nd
connecting-character:
    A Unicode character of the class Pc
    A unicode-escape-sequence representing a character of the class Pc
formatting-character:
    A Unicode character of the class Cf
    A unicode-escape-sequence representing a character of the class Cf 

连字符减号属于 Pd 类,因此在上面不允许使用。

这确实是为什么在名称中允许 Pd 类的东西是一个坏主意的原因之一:

int x-y = 3;
int x = 10;
int y = 2;
int z = x-y; //3 or 8? Impossible to tell if the first line were allowed.
于 2012-08-31T13:30:41.437 回答
6

简短的回答:你不能。

.NET 支持任意成员名称,包括在 CIL 语言中非法的成员名称。但是,如果一种语言不支持它,那么您就不能使用它,这包括连字符和破折号。出于这个原因,C# 中的自动属性在其隐藏的支持字段名称中<使用。>

C# 确实允许您在关键字前面加上@符号,以允许您将其用作变量或成员名称(例如public int @class,或者public void @struct(int @interface),但这是为了消费者的利益。

但是,在任何情况下,c# 都不允许您在名称中使用连字符,因为该字符用于一元否定和二元减法运算符。名称flv270-p被解释为“flv270减号p”。

于 2012-08-31T13:29:01.197 回答
3

实际上你可以使用这样的代码

public string flv270_p { get; set; }

并返回一个用“_”替换“-”的重新格式化的类。

例如,如果你有这个,

var jsonstring = ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(SMS), "/sms/json"), new Dictionary<string, string>()
        {
            {"from", request.from},
            {"to", request.to},
            {"text", request.text}
        });

        return JsonConvert.DeserializeObject<SMSResponse>(jsonstring);

做这个,

var jsonstring = ApiRequest.DoRequest(ApiRequest.GetBaseUriFor(typeof(SMS), "/sms/json"), new Dictionary<string, string>()
        {
            {"from", request.from},
            {"to", request.to},
            {"text", request.text}
        }).Replace("-","_");

        return JsonConvert.DeserializeObject<SMSResponse>(jsonstring);
于 2015-07-06T22:11:26.100 回答
1

其他答案很好地解释了为什么你不能 do exacwhat you are asking for. That being said, I have encountered situations where I wanted my object metadata to have hyphens for the purpose of formulating descriptive URLs that are optimized for SEO. The Attribute approach you used in the question works, but you can also use conventions to indicate this kind of customization without attributes. One suggestion is to preprocess member names to use either PascalCase and insert a hyphen delimiter in between words. This might cause too much metadata alteration. Another option is to use an underscore '_', which is a legal character in C# names, and to replace underscores with hyphens in your preprocessing. Your question doesnt really say enough about your use case to indicate how this would be implemented/injected in your metadata context, but hopefully these ideas are helpful when you're designing your api.

于 2012-08-31T15:29:00.313 回答
1

flv-270p在 C# 中不是有效的变量名。每当您在代码中键入类似的内容时,编译器都会认为您正在尝试执行“flv270 minus p”:)

于 2012-08-31T13:30:09.360 回答