4

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.

2
  • 3
    The error is pretty clear, you want to deserialize an array of objects into a single object. Use a collection for deserialization like ICollection<Sale> or ensure the json contains no array Commented Dec 16, 2017 at 11:21
  • 1
    BTW you do have a coder friend: google for json.net Cannot deserialize the current JSON array Commented Dec 16, 2017 at 11:24

2 Answers 2

4

You should deserialize it as collection;

JsonConvert.DeserializeObject<List<Sale>>(jsonstring);

And your linq query looks like;

var shapedData = data.Select(x =>
    new
    {
       ID = x.ID,
       userInf = x.UserInf.Name,
       price = x.Price,
       kdv = x.Kdv,
       total = x.Total,
       note = x.note
    }).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

yes i think it will work but how I'm going to use ID = data.ID, userInf = data.UserInf.Name,
1

can you try like this , dont make use of var as you already know return type

List<Sale> data = JsonConvert.DeserializeObject<List<Sale>>(jsonstring);

as your string return array of data

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.