Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Json deserialize error
Message
From
06/05/2016 14:58:01
 
 
To
06/05/2016 14:29:01
James Blackburn
Qualty Design Systems, Inc.
Kuna, Idaho, United States
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01635955
Message ID:
01635960
Views:
180
You can do this natively and skip Json.NET
using (var client = new HttpClient())
{
    client.BaseAddress - new Uri(your URL goes here);
    client.DefaultRequestHeaders.Accept.Clear(); 
    client.DefaultRequestHeaders.Accept.add(new MediaTypeWithQualityHeaderValue("application/json");
    HttpResponse response = await client.GetAsync(the url parameters go here);
    if (response.IsSuccessStatusCode)
    {
        var result = response.Content.ReadAsAsync<DetailsHeader>();
    }
}
>VS 2015 and .net 4.5 I am also using the xamarin plugin.
>
>>What version of .NET?
>>
>>
>>>Hi all
>>>
>>>Probably a newbe question but I can't get this to work. I am trying to consume an API return nested json. I have tried the following code and get this error.
>>>
>>>Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[m2m.Model.DetailsHeader]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
>>>
>>>
>>>            HttpClient client = new HttpClient();
>>>            client.BaseAddress = new Uri(url);
>>>            var response = await client.GetAsync(client.BaseAddress);
>>>            response.EnsureSuccessStatusCode();
>>>            var JsonResult = response.Content.ReadAsStringAsync().Result; // I have confirmed that the raw json is in JsonResult.
>>>            
>>>            var promotemp = JsonConvert.DeserializeObject<List<DetailsHeader>>(JsonResult);
>>>
>>>namespace m2m.Model
>>>{
>>>    }
>>>
>>>    public class DetailsHeader
>>>    {
>>>        public int autocouponlinkpk { get; set; }
>>>        public DateTime enddate { get; set; }
>>>        public string market_descrp { get; set; }
>>>        public string globallogo { get; set; }
>>>        public List<PromoDetails> details { get; set; }
>>>    }
>>>
>>>    public class PromoDetails
>>>    {
>>>        public long autocouplinkpage2pk { get; set; }
>>>        public string image_fullurl { get; set; }
>>>        public string group_descrp { get; set; }
>>>    }
>>>}
>>>
>>>
>>>
>>>and here is the json
>>>
>>>
>>>{
>>>  "autocouponlinkpk": 170,
>>>  "enddate": "2016-06-30",
>>>  "market_descrp": "My test",
>>>  "globallogo": "http://subscriberimages/47/fkpzlkxift-1423092258.jpg",
>>>  "details": [
>>>    {
>>>      "autocouplinkpage2pk": 82,
>>>      "image_fullurl": "http://marketImgs/47/xhkppjcxey-1445635420.png",
>>>      "group_descrp": "This is page 2"
>>>    },
>>>    {
>>>      "autocouplinkpage2pk": 83,
>>>      "image_fullurl": "http://marketImgs/47/movjznnosp-1445700931.jpg",
>>>      "group_descrp": "page 2 of page 2."
>>>    },
>>>    {
>>>      "autocouplinkpage2pk": 84,
>>>      "image_fullurl": "http://marketImgs/47/vbzkznsbkg-1445700969.jpg",
>>>      "group_descrp": "page 3 of page 2"
>>>    }
>>>  ]
>>>}
>>>
>>>
>>>What am I doing wrong?
>>>
>>>Thanks.
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform