2

サーバー側のメソッドの1つにJSONのPOSTを実行するAPIを使用しています。

その JSON の構造にマップするいくつかの C# クラスの作成に取り組んでいます。私の問題は、私に投稿されたフィールドの1つが「オブジェクト」という名前で、その文字列であることです。

これは、私に送信された JSON の例です....

[
{
    "subscription_id": "1",
    "object": "user",
    "object_id": "1234",
    "changed_aspect": "media",
    "time": 1297286541
},
{
    "subscription_id": "2",
    "object": "tag",
    "object_id": "nofilter",
    "changed_aspect": "media",
    "time": 1297286541
},]

これが私の問題です。object は予約語であるため、モデル バインダーに json の "object" プロパティを取得し、C# クラスで別の名前にマップするように指示するにはどうすればよいですか?

 public class InstagramUpdate
{
    public string subscription_id { get; set; }
    public string object_id { get; set; }
    public string object { get; set; } //<-- what should I do here??
    public string changed_aspect { get; set; }
    public int time { get; set; }
}

これが理にかなっていると思いますか?

ありがとう!

4

1 に答える 1

5

ここにも答えがあるだけです:

変数/属性名として設定する場合は、C# の予約済みキーワードの前に「@」を付けてください。

すなわち:

public class InstagramUpdate
{
    public string subscription_id { get; set; }
    public string object_id { get; set; }
    public string @object { get; set; } //object will be here the prop name
    public string changed_aspect { get; set; }
    public int time { get; set; }
}
于 2012-09-24T20:38:30.333 に答える