Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {“name”:“value”}) to deserialize correctly

前端 未结 5 1628
耶瑟儿~
耶瑟儿~ 2020-11-22 16:19

I have this JSON:

[
    {
        \"Attributes\": [
            {
                \"Key\": \"Name\",
                \"Value\": {
                    \"Value         


        
5条回答
  •  抹茶落季
    2020-11-22 17:09

    If one wants to support Generics (in an extension method) this is the pattern...

    public  static List Deserialize(this string SerializedJSONString)
    {
        var stuff = JsonConvert.DeserializeObject>(SerializedJSONString);
        return stuff;
    }
    

    It is used like this:

    var rc = new MyHttpClient(URL);
    //This response is the JSON Array (see posts above)
    var response = rc.SendRequest();
    var data = response.Deserialize();
    

    MyClassType looks like this (must match name value pairs of JSON array)

    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
     public class MyClassType
     {
        [JsonProperty(PropertyName = "Id")]
        public string Id { get; set; }
    
        [JsonProperty(PropertyName = "Name")]
        public string Name { get; set; }
    
        [JsonProperty(PropertyName = "Description")]
        public string Description { get; set; }
    
        [JsonProperty(PropertyName = "Manager")]
        public string Manager { get; set; }
    
        [JsonProperty(PropertyName = "LastUpdate")]
        public DateTime LastUpdate { get; set; }
     }
    

    Use NUGET to download Newtonsoft.Json add a reference where needed...

    using Newtonsoft.Json;
    

提交回复
热议问题