反序列化复杂的json字符串

时间:2016-05-09 10:47:44

标签: c# json entity-framework

我有以下JSON字符串:

{
   "TeamName":"Team",
   "FirstColor":"",
   "ContactPerson":"Contact",
   "ContactNo":"0101010101010",
   "Email":"admin@example.com",
   "Tournaments":[
      {
         "Tournament":{
            "TournamentID":1,
            "TournamentName":"Tournament 1",
            "TournamentFee":0,
            "IsSelected":true
         }
      },
      {
         "Tournament":{
            "TournamentID":2,
            "TournamentName":"Tournament 2",
            "TournamentFee":0,
            "IsSelected":true
         }
      },
      {
         "Tournament":{
            "TournamentID":3,
            "TournamentName":"Tournament 3",
            "TournamentFee":0,
            "IsSelected":true
         }
      },
      {
         "Tournament":{
            "TournamentID":4,
            "TournamentName":"Tournament 4",
            "TournamentFee":0,
            "IsSelected":false
         }
      },
      {
         "Tournament":{
            "TournamentID":5,
            "TournamentName":"Tournament 5",
            "TournamentFee":0,
            "IsSelected":false
         }
      },
      {
         "Tournament":{
            "TournamentID":6,
            "TournamentName":"Tournament 6",
            "TournamentFee":0,
            "IsSelected":false
         }
      },
      {
         "Tournament":{
            "TournamentID":7,
            "TournamentName":"Tournament 7",
            "TournamentFee":0,
            "IsSelected":false
         }
      }
   ]
}

我在c#使用以下Entity Framework个类:

[Table("TournamentForm")]
    public class TournamentForm
    {
        [Key]
        public long TournamentFormID { get; set; }

        [JsonProperty("TeamName")]
        public string TeamName { get; set; }

        [JsonProperty("FirstColor")]
        public string FirstColor { get; set; }

        [JsonProperty("SecondColor")]
        public string SecondColor { get; set; }

        [JsonProperty("TeamManager")]
        public string TeamManager { get; set; }

        [JsonProperty("ContactPerson")]
        public string ContactPerson { get; set; }

        [JsonProperty("ContactNo")]
        public string ContactNo { get; set; }

        [JsonProperty("Email")]
        public string Email { get; set; }        

        [JsonProperty(PropertyName = "Tournaments")]
        public virtual List<Tournament> Tournaments { get; set; }
}

[Table("Tournaments")]
    [JsonObject("Tournament")]
    public class Tournament
    {
        [Key]
        [JsonProperty(PropertyName = "TournamentID")]
        public long TournamentID { get; set; }

        [JsonProperty(PropertyName = "TournamentName")]
        public string TournamentName { get; set; }

        [JsonProperty(PropertyName = "TournamentFee")]
        public decimal TournamentFee { get; set; }    

        [NotMapped]
        [JsonProperty(PropertyName = "IsSelected")]
        public bool IsSelected { get; set; }

    }

JSON字符串根目录中的属性正确映射到TournamentForm类,但Tournaments数组未映射到List Tournaments存在于TournamentForm类中。我使用以下代码反序列化JSON字符串:

 public void SubmitTournamentRegistrationForm(string formData)
 {
    TournamentForm respone = JsonConvert.DeserializeObject<TournamentForm>(formData);
 }

Everthing似乎已正确映射,但我仍未在List的{​​{1}}中获取数据。我的代码在哪里出错?

0 个答案:

没有答案