Deserializing JSON when fieldnames contain spaces

后端 未结 4 1928
野性不改
野性不改 2020-12-31 05:11

I\'m writing a tool to read JSON files. I\'m using the NewtonSoft tool to deserialize the JSOn to a C# class. Here\'s an example fragment:

 \"name\": \"Fubar         


        
4条回答
  •  萌比男神i
    2020-12-31 06:00

    System.Text.Json

    If you're using System.Text.Json, the equivalent attribute is JsonPropertyName:

    [JsonPropertyName(".net version")]
    public string DotNetVersion { get; set; }
    

    Example below:

    public class Data
    {
        public string Name { get; set; }
    
        [JsonPropertyName(".net version")]
        public string DotNetVersion { get; set; }
    
        [JsonPropertyName("binding type")]
        public string BindingType { get; set; }
    }
    
    // to deserialize
    var data = JsonSerializer.Deserialize(json);
    

提交回复
热议问题