Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Json deserialize error
Message
De
07/05/2016 04:11:26
 
 
À
06/05/2016 13:13:48
James Blackburn
Qualty Design Systems, Inc.
Kuna, Idaho, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01635955
Message ID:
01635980
Vues:
55
>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.

As per the error : the Json is not an array so can't be converted to a list. Use:
var promotemp = JsonConvert.DeserializeObject<DetailsHeader>(JsonResult);
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform