2016-11-23 8 views
0

Web APIでC#を使用しています。私は(HTTPリクエストのBODYに配置された)、このデータと、このメソッドを呼び出そうと、今C#Web API:別の名前でデシリアライズが動作しない

public class CompareOutput 
    { 
     public CompareOutput(int wc, int source_start, int source_end, int suspected_start, int suspected_end) 
     { 
      this.WordsCount = wc; 
      this.SourceStartChar = source_start; 
      this.SourceEndChar = source_end; 
      this.SuspectedStartChar = suspected_start; 
      this.SuspectedEndChar = suspected_end; 
     } 

     public CompareOutput() 
     { 

     } 

     [JsonProperty("wc")] 
     public int WordsCount { get; set; } 

     [JsonProperty("SoS")] 
     public int SourceStartChar { get; set; } 

     [JsonProperty("SoE")] 
     public int SourceEndChar { get; set; } 

     [JsonProperty("SuS")] 
     public int SuspectedStartChar { get; set; } 

     [JsonProperty("SuE")] 
     public int SuspectedEndChar { get; set; } 

     public override string ToString() 
     { 
      return string.Format("{0} -> {1} | {2} -> {3}", this.SourceStartChar, this.SourceEndChar, this.SuspectedStartChar, this.SuspectedEndChar); 
     } 
    } 

::これは私のデータモデルである

[System.Web.Http.HttpPost] 
public HttpResponseMessage Test(CompareOutput model) 
{ 
    return new HttpResponseMessage(HttpStatusCode.OK); 
} 

:私は、このサーバーメソッドを持っている

{ 
    "wc":11, 
    "SoS":366, 
    "SoE":429, 
    "SuS":393, 
    "SuE":456 
} 

この呼び出しは機能しません。すべてのメンバーがデフアル値を取得しています(「0」)。int私は、このHTTP-BODYを送信しようとすると、それは良い働いて

:私はそれから理解することができます

{ 
    "WordsCount":11, 
    "SourceStartChar":366, 
    "SourceEndChar":429, 
    "SuspectedStartChar":393, 
    "SuspectedEndChar":456 
} 

を - (JsonPropertry属性を持つモデルで定義されている)の代替名が働いていません。

どうすれば解決できますか?

ありがとうございます。

+0

があれば、各プロパティにし、[データメンバー(名前は=「WC」)]などの上部にある[のDataContract]を持つクラスをマークアップしてみてくださいと表示さでしたそれは動作しますか? – RoguePlanetoid

+0

はい、私はそれを試しました。これは私の最初のショットでした。何の違いもありません。 – No1Lives4Ever

+0

少し類似した問題があります:http://stackoverflow.com/questions/26882986/overwrite-json-property-name-in-c-sharp – owczarek

答えて

0

あなたの特性をこのように飾るしよう...

[JsonProperty(PropertyName = "wc")] 
public int WordsCount { get; set; } 

[JsonProperty(PropertyName = "SoS")] 
public int SourceStartChar { get; set; } 

[JsonProperty(PropertyName = "SoE")] 
public int SourceEndChar { get; set; } 

[JsonProperty(PropertyName = "SuS")] 
public int SuspectedStartChar { get; set; } 

[JsonProperty(PropertyName = "SuE")] 
public int SuspectedEndChar { get; set; } 
+0

まだ動作しません。私のために同じ行動。 – No1Lives4Ever

関連する問題