.NET NewtonSoft JSON deserialize map to a different property name

后端 未结 5 675
情歌与酒
情歌与酒 2020-11-22 09:20

I have following JSON string which is received from an external party.

{
   \"team\":[
      {
         \"v1\":\"\",
         \"attributes\":{
            \"         


        
5条回答
  •  遇见更好的自我
    2020-11-22 09:37

    Json.NET has a JsonPropertyAttribute which allows you to specify the name of a JSON property, so your code should be:

    public class TeamScore
    {
        [JsonProperty("eighty_min_score")]
        public string EightyMinScore { get; set; }
        [JsonProperty("home_or_away")]
        public string HomeOrAway { get; set; }
        [JsonProperty("score ")]
        public string Score { get; set; }
        [JsonProperty("team_id")]
        public string TeamId { get; set; }
    }
    
    public class Team
    {
        public string v1 { get; set; }
        [JsonProperty("attributes")]
        public TeamScore TeamScores { get; set; }
    }
    
    public class RootObject
    {
        public List Team { get; set; }
    }
    

    Documentation: Serialization Attributes

提交回复
热议问题