2

I have this json file:

{
  "timestamp": 1557323147422,
  "change_id": 11687520784,
  "data": [
      [ "new", 5775.0, 16530.0 ],
      [ "new", 5774.5, 360.0 ]
  ]
}

I need to set up a class to deserialize it, but the data array is causing me a problem.

I tried to map data to:

List<(string, double, double)>

but that doesn't work. List works, but then it's just pushing the problem one step away.

I can map it to

List<dynamic>

and then I get a list of JArray that I need to parse individually.

What I need is to be able to map it to some class that has a string and 2 doubles.

9
  • maybe tuple ? Commented May 8, 2019 at 14:04
  • @demo well he tried a tuple with List<(string, double, double)> Commented May 8, 2019 at 14:07
  • yes, that doesn't work: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Tuple`3[System.String,System.Double,System.Double]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. Commented May 8, 2019 at 14:07
  • I can do a List<List<string>> but then I have to parse the doubles after that Commented May 8, 2019 at 14:09
  • 1
    Nope. In the future if you will use .NET Core >= 3.0, pls, read about new MS .Net json serializer which will replace JSON.NET. Commented May 8, 2019 at 14:50

2 Answers 2

0

You can use http://json2csharp.com/

I generated this code:

public class RootObject
{
    public long timestamp { get; set; }
    public long change_id { get; set; }
    public List<List<object>> data { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

yes, that's what I wrote about in the question, it just shifts the problem to parsing the object.
0

Your array is still an array of object in JSON, it is neither a tuple nor a type.

So List<List<object>> (or IEnumerable<IEnumerable<object>>) seems to be the only option.

1 Comment

or object[][] ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.