C# json uzantılı dosyadan veri okuma

ismailaydogdu03
12-06-2019, 23:33   |  #1  
ismailaydogdu03 avatarı
OP Taze Üye
Teşekkür Sayısı: 0
1 mesaj
Kayıt Tarihi:Kayıt: Haz 2019

{
    "stockList": [{
        "stok_kodu": "BOSCH 0001107403",
        "stok_adi": "MARS MOTORU 12V 1,1 KW MB C180, C200",
        "marka": "BOSCH",
        "uretici_kodu": "21331",
        
    }, {
        "stok_kodu": "BOSCH 0001107492",
        "stok_adi": "MARS MOTORU 12V ASTRA 1,4 16V VVTI 09-> (0001107408)",
        "marka": "BOSCH",
        "uretici_kodu": "1202110",
  
    }]
}
Bu içerikte bir json dosyam var c# ile bu satırlara ulaşmak istiyorum


C# Kodlarım= >>


 public class Person
    {
        
        public string stok_kodu;
        public string stok_adi;
        
    }

protected void Button1_Click(object sender, EventArgs e)
    {

        using (System.IO.StreamReader _StreamReader = new System.IO.StreamReader(@"C:\deneme2.json"))
        {
            string jsonData = _StreamReader.ReadToEnd();
            List<Person> listPerson = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Person>>(jsonData);

            foreach (var _Person in listPerson)
            {
                 Label1.Text +=" "+_Person.stok_kodu + " "+ _Person.stok_adi +" <br />";
            }
        }

    }


Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Bin_Default+Person]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.


hatasını alıyorum yarımcı olabilirmisiniz

White-Fox
26-04-2020, 12:04   |  #2  
White-Fox avatarı
Yıllanmış Üye
Teşekkür Sayısı: 155
1,104 mesaj
Kayıt Tarihi:Kayıt: Eyl 2010

Hata açıkca söylemiş, orada bir array var bunu düzgün bir biçimde serialize etmek için senin bana verdiğin örnek sınıfta array yok diyor. :)