I have a json string like this;
[
{
"ID": 123456789,
"userInf": {
"Name": "NameSurname1",
"Adress": "example adress"
},
"price": "3898.30",
"kdv": "701.69",
"total": "4599,99",
"note": "example note"
},
{
"ID": 98756431,
"userInf": {
"Name": "NameSurname2",
"Adress": "example address2"
},
"price": "1116.10",
"kdv": "82.90",
"total": "1199.00",
"note": "example note2"
}
]
And build up classes like this;
public partial class Sale
{
public long ID { get; set; }
public UserInf UserInf { get; set; }
public string Price { get; set; }
public string Kdv { get; set; }
public string Total { get; set; }
public string note { get; set; }
}
public partial class UserInf
{
public string Name { get; set; }
public string Adress { get; set; }
}
And I call json with this code and deserialize;
var data = JsonConvert.DeserializeObject<Sale>(jsonstring);
var shapedData = Enumerable.Range(0, 1).Select(x =>
new
{
ID = data.ID,
userInf = data.UserInf.Name,
price = data.Price,
kdv = data.Kdv,
total = data.Total,
note= data.Note
}).ToList();
DataTable dt = ToDataTable(shapedData);
dataGridView1.DataSource = dt;
and I get error. But if I change my json and cut the second half and delete the [,] symbols code just works perfectly fine. I need to deserialize multiple like above and tried multiple ways to deserialize but this is the closest way I get so far.
I get the error on this line ;
var data = JsonConvert.DeserializeObject<Sale>(jsonstring);
Error is
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type .. and so.
I know I'm missing something very basic but I have no coder friend to ask. I would be grateful if you could suggest a better way to do it.