1

I want to deserialize a JSON array using Newtonsoft to class.

The JSON array like (Api returns that)

[1,"1",true,2,3]

To class

class MyObject {
    public int Number { get; set; }
    public string Data { get; set; }
    public bool IsIt { get; set; }
}

Is there any way except creating the constructor like

public MyObject(object[] array) {
    Number = (int)array[0];
    Data = (string)array[1];
    IsIt = (bool)array[2];
}
6
  • I'd suggest to change the API if you can. Commented Oct 13, 2017 at 6:54
  • 1
    In short - no, API is terrible, if it returns such array of objects Commented Oct 13, 2017 at 6:55
  • I know that API is terrible, but I need to work with it. Commented Oct 13, 2017 at 6:56
  • It's not a bad API, just use JsonConvert.DeserializeObject and read about dynamic objects. Commented Oct 13, 2017 at 7:01
  • 3
    You could use ObjectToArrayConverter<MyObject> from C# JSON.NET - Deserialize response that uses an unusual data structure. Commented Oct 13, 2017 at 7:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.