Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

WinForms Error Cannot deserialize the current JSON array (e.g. [1,2,3]) into type

i have a code like this:

Dictionary<string, Diskop> data = JsonConvert.DeserializeObject<Dictionary<string, Diskop>>(res.Body.ToString());

and this is the Diskop class:

internal class Diskop : Dictionary<string, Diskop>
{
    public string isim { get; set; }
    public int no { get; set; }
    public int puan { get; set; }
}

and i get that error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Newtonsoft.Json.JsonSerializationException: ‘Cannot deserialize the current JSON array (e.g. [1,2,3]) into type ‘System.Collections.Generic.Dictionary`2[System.String,Turuncu_Uygulama.Diskop]’ because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

here is the json

[ {

"isim" : "AHMET UTKU GÖKSAL",
"no" : 929,
"puan" : 96
}, {
"isim" : "AHMET YASİR YILDIZ",
"no" : 969,
"puan" : 95
}, {
"isim" : "AKİF ENES ÖZDEMİR",
"no" : 953,
"puan" : 108
}, {
"isim" : "ALİ AKTÜRK",
"no" : 910,
"puan" : 111
}

>Solution :

Diskop should look like:

internal class Diskop
{
    [JsonProperty("isim")]
    public string Isim { get; set; }

    [JsonProperty("no")]
    public int No { get; set; }

    [JsonProperty("puan")]
    public int Puan { get; set; }
}

Deser should perhaps look like:

var data = JsonConvert.DeserializeObject<Diskop[]>(res.Body.ToString());

Or like:

var data = JsonConvert.DeserializeObject<List<Diskop>>(res.Body.ToString());

Because your json represents an array of Diskops:

[ 
{ "isim" : "AHMET UTKU GÖKSAL", "no" : 929, "puan" : 96 }, 
{ "isim" : "AHMET YASİR YILDIZ", "no" : 969, "puan" : 95 }, 
{ "isim" : "AKİF ENES ÖZDEMİR", "no" : 953, "puan" : 108 },
{ "isim" : "ALİ AKTÜRK", "no" : 910, "puan" : 111 }
]

You’d use a Dictionary<string, Diskop> if it looked like a mapping of diskops:

{ 
"a": { "isim" : "AHMET UTKU GÖKSAL", "no" : 929, "puan" : 96 }, 
"b": { "isim" : "AHMET YASİR YILDIZ", "no" : 969, "puan" : 95 }, 
"c": { "isim" : "AKİF ENES ÖZDEMİR", "no" : 953, "puan" : 108 },
"d": { "isim" : "ALİ AKTÜRK", "no" : 910, "puan" : 111 }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading