c# - Serializing a JSON array to an instance of a class -
if have json data such as:
{ "datapoints":[ [null, 1234567890, "point1"], [null, 1234567890, "point2"] ] } is there way deserialize data following classes:
public class result { public datapoint[] datapoints {get;set;} } public class datapoint { public string name {get;set;} public string value {get;set;} public datetime timestamp {get;set;} } can specify index of array member serialize property?
if open custom deserialization using json.net
string json = @"{""datapoints"":[ [null, 1234567890, ""point1""], [null, 1234567890, ""point2""] ]}"; var result = (jobject)jsonconvert.deserializeobject(json); var datapoints = result["datapoints"] .select(t => new datapoint() { timestamp = string.isnullorempty(t[0].tostring()) ? datetime.minvalue : datetime.parse(t[0].tostring()), value = t[1].tostring(), name= t[2].tostring() }) .toarray(); ps: timestamp may need more work depending on format.
Comments
Post a Comment