Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize JSON (Json.NET)

I need a little help with json deserialization. It's the first time I'm using Json, so I have just a little knowledge about it.

I get the following string using a webclient:

[{"name": "somename", "data": [[72, 1504601220], [null, 1504601280], ..., [125, 1504605840]]}]

and tried to serialize it with

JsonConvert.DeserializeObject<TestObject>(jsonstring)

My class looks like this:

public class TestObject
{
    [JsonProperty(PropertyName = "name")]
    public string TargetName { get; set; }

    [JsonProperty(PropertyName = "data"]
    public List<?????> DataPoints {get; set;}
}

How do I need to design my class to get the data values in some kind of collection so each entry contains the two values inside a bracket?

Thx for your patience and help!

like image 909
Erik T. Avatar asked Feb 02 '26 12:02

Erik T.


2 Answers

Your data is a list of arrays containing nullable integers (by the looks of it)

[JsonProperty(PropertyName = "data"]
public List<int?[]> DataPoints {get; set;}
like image 200
Jamiec Avatar answered Feb 04 '26 02:02

Jamiec


Try this website: http://json2csharp.com/ This website can save a lot of your time if you have just a plain JSON text. It helps you convert it to C# object, although you still have to double check it.

var data = "[{\"name\": \"somename\", \"data\": [[72, 1504601220], [null, 1504601280], [125, 1504605840]]}]";
var obj = JsonConvert.DeserializeObject<List<TestObject>>(data);

public class TestObject
{
    [JsonProperty(PropertyName = "name")]
    public string TargetName { get; set; }
    [JsonProperty(PropertyName = "data")]
    public List<int?[]> DataPoints { get; set; }
}
like image 38
Rawitas Krungkaew Avatar answered Feb 04 '26 00:02

Rawitas Krungkaew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!