Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Json deserialize error
Message
 
To
06/05/2016 13:13:48
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:
01636073
Views:
68
The issue here is that you're deserializing a list into a dictionary or vice versa. You need to make sure you use the right data type that matches the export. An array maps to an array, list or hashset. Dictionaries serialize different using key/value pair syntax.

+++ Rick ---

>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.
+++ Rick ---

West Wind Technologies
Maui, Hawaii

west-wind.com/
West Wind Message Board
Rick's Web Log
Markdown Monster
---
Making waves on the Web

Where do you want to surf today?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform