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:
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 }
CodePudding user response:
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 }
}