Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Json deserialize error
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01635955
Message ID:
01636320
Views:
65
Yes, but the way JSON.NET deserializes and serializes Dictionaries is as a an array of key value objects (assuming that's what the .NET interface represents).

I've never liked the way this works, but it is the standard way htat JavaScript represents the equivalence of dictionaries. It'd be much clearer if dictionaries serialized into objects that contain key and value properties, but that does get more verbose.


+++ RIck ---


>To be accurate: the json is an object containing a list of name/value pairs. In your case the 'details' item is an array containing three objects, each with it's own name/value pair collection :-}
>
>>Thanks Rick. Part of the json is a list but I had my List in the wrong place. I am just getting started with c# so a lot to learn.
>>
>>>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
Reply
Map
View

Click here to load this message in the networking platform