2

If I have JSON data such as:

{
"datapoints":[ [null, 1234567890, "point1"], [null, 1234567890, "point2"] ]
}

Is there a way to deserialize that data into the 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 I specify the index of which array member to serialize into which property?

2 Answers 2

2

If you are open to some 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 some more work depending on the format.

Sign up to request clarification or add additional context in comments.

1 Comment

This is pretty much what I'm doing now. I just wondered if there was a more direct method to deserialize rather than needing to enumerate the list again to parse the array values.
0

It's hard to say exactly, since you're not specifying how you're deserializing the data.

If using external libraries is a possibility, I'd recommend using Json.Net - it's a lot nicer to use than System.Web.Script.Serialization.JavaScriptSerializer.

Edit to address (very valid!) concerns - as mentioned elsewhere on SO, if you include Json.Net, you'd be able to say JArray a = JArray.Parse(datapoints); to load up your arrays, and then you'd manually new up new DataPoints for each new array item and populate it with values from the expected slots. I'm not aware of attributes that you could decorate your class properties with that would make that any prettier. You could likely use its Linq to Json to make this a less ugly affair.

4 Comments

I'm open to suggestions. Currently, I'm using the DataContractJsonSerializer (different from JavaScripSerializer).
"Use an external library" really isn't an answer.
Per your edit, I can also just use DataContractJsonSerializer to parse data into object[] and not add the dependency or learning curve of Json.Net. I wanted to know if something could directly translate between the array and my class.
@scottm - quite right about being able to use DataContractJsonSerializer in the same way - you'd be able to use it to get to the intermediate multidimensional array of strings stage and from there, use Linq to instantiate a bunch of new objects. I just thought I'd mention Json.Net as JavaScriptSerializer has, um, character.

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.