How can I parse the JSON object below using DataContractJsonSerializer
in C#?
I will need to define a class to hold the below JSON data, which includes an array of arrays of primitives of mixed types (string and integer):
Body:
{
"status": "failure",
"staticdata": [
[
"2013-06-01",
123
],
[
"2013-06-02",
234
],
[
"2013-06-03",
345
],
...
]
}
I tried the below answer and tried to read through DataContractJsonSerializer
,
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject));
object objResponse = jsonSerializer.ReadObject(response);
RootObject jsonResponse = objResponse as RootOject;
foreach (object[] sd in jsonResponse.staticdata)
{
foreach (object o in sd)
{
//Value val = v as Value;
Value val = (Value)Convert.ChangeType(o, typeof(Value));
log.Info("date: " + val.date);
log.Info("crashCount: " + val.longValue);
}
}
but in converttype from object to Value is crashing, am I missing something here.
Value is below class:
[DataContract]
public class Value
{
[DataMember(Name = "date")]
public string date { get; set; }
[DataMember(Name = "longValue")]
public long longValue{ get; set; }
}
modified code read Values (IgnoreDataMember Values), and then could able to read as below: is this the right approach?
object objResponse = jsonSerializer.ReadObject(response);
RootObject jsonResponse = objResponse as RootOject;
foreach (Value in jsonResponse.Values)
{
log.Info("date: " + val.date);
log.Info("longValue: " + val.longValue);
}