Hi I am currently doing my final year project and nearly at the end. However, I am stuck on one small thing which is parsing the below JSON string in C#.
My Bad... copied and pasted an excerpt from the JSON file. Here is the whole JSON string which is valid when I checked jsonutils.org
{
"activities-steps" : [
{
"dateTime" : "2016-02-10",
"value" : "2624"
}
],
"activities-steps-intraday" : {
"dataset" : [
{
"time" : "00:00:00",
"value" : 0
},
{
"time" : "00:01:00",
"value" : 0
},
{
"time" : "00:02:00",
"value" : 0
},
{
"time" : "00:03:00",
"value" : 0
},
{
"time" : "00:04:00",
"value" : 0
},
{
"time" : "00:05:00",
"value" : 0
},
{
"time" : "00:06:00",
"value" : 0
},
{
"time" : "00:07:00",
"value" : 0
},
{
"time" : "00:08:00",
"value" : 0
},
{
"time" : "00:09:00",
"value" : 0
},
{
"time" : "00:10:00",
"value" : 0
},
{
"time" : "00:11:00",
"value" : 0
},
{
"time" : "00:12:00",
"value" : 0
},
{
"time" : "00:13:00",
"value" : 0
},
{
"time" : "00:14:00",
"value" : 0
},
{
"time" : "00:15:00",
"value" : 0
},
{
"time" : "00:16:00",
"value" : 0
},
{
"time" : "00:17:00",
"value" : 0
}
],
"datasetInterval" : 1,
"datasetType" : "minute"
}
}
I need the value of the dateTime in the first JSON object and then the values of the time and value items in the second object. I have implemented the below code however it keeps on giving me a NullReferenceException.
These methods are used in order to help parse the JSON objects
public class RootObject
{
public Activity activity { get; set; }
public ActivityGranular activity_granular { get; set; }
}
public class Activity
{
public string dateTime { get; set; }
public string value { get; set; }
}
public class ActivityGranular
{
public List<ActivityGranularDatapoint> dataset { get; set; }
public int datasetGranularity { get; set; }
public string datasetGranularityType { get; set; }
}
public class ActivityGranularDatapoint
{
public string time { get; set; }
public int value { get; set; }
}
MainCode in the Main Class which when executed returns null in both root elements in the root objects class above and gives a NullReferenceExceptions
resultsJSON.Replace("-", "_");
root = JsonConvert.DeserializeObject<RootObject>(resultsJSON);
activity = root.activites_steps;
activityGranular = root.activities_steps_intraday;
for (int a = 0; a < activityGranular.dataset.Count; a++)
{
activityGranularDatapoint[a].time = activityGranular.dataset[a].time;
activityGranularDatapoint[a].value = activityGranular.dataset[a].value;
}
All help is much appreciated :)