0
\$\begingroup\$

I am wanting to build the C# data object to represent the following JSON. I am using unity's JsonUtility. I have used the JSONUtility before but only with a simpler JSON structure. However I am not sure nesting can be done as I have been searching for examples without any luck.

{
"datacharts":[{
    "0000000009":[{ 
            "dateTime": "2016-09-14 00:14:20",
            "seconds": "60.50"
        },
        {
            "dateTime": "2016-09-14 00:15:20",
            "seconds": "57.23"
        },{ 
            "dateTime": "2016-09-14 00:18:20",
            "seconds": "61.230"
        },
        {
            "dateTime": "2016-09-14 00:23:20",
            "seconds": "55.875"
        }
    ] 
 }]
}

My current C# model.

[System.Serializable]
public struct Drivers
{
        public DateTime[] dataTime; //time stamp unique enough to group each points?
        public float[] seconds;
} 

[System.Serializable]
public class DataCharts {
    public List<Drivers> driverIds;
}

[System.Serializable]
public class DataList{

public List<DataCharts> availableData;
}

I'm not sure this answer will help me as I don't understand what it's wrapping. https://forum.unity3d.com/threads/how-to-load-an-array-with-jsonutility.375735/#post-2585129

Should my Json be modal differently? I am wanting to save driver times to display in a bar chart. So every driver would have a timestamp and the lap time.

UPDATE: after reading this question/answer Communicate Unity application via a series of Json

I've remodelled the json and c#. Json

{
    "driverId": "0000000009",
    "dataPoints": [{
                "dateTime": "2016-09-14 00:14:20",
                "seconds": "60.50"
            },{
                "dateTime": "2016-09-14 00:15:20",
                "seconds": "57.23"
            },{ 
                "dateTime": "2016-09-14 00:18:20",
                "seconds": "61.230"
            },
            {
                "dateTime": "2016-09-14 00:23:20",
                "seconds": "55.875"
        }]
} 

C#

[Serializable]
public struct Drivers
{
    public string driverId;
    public List<DataPoints> points;
} 

[Serializable]
public class DataPoints {
    public string dateTime;
    public string seconds;
}

[Serializable]
public class DataList{
    public List<Drivers> availableData;
}
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I've come to realize the answer is that it's not meant to do nesting. Unity JSONUtility is for retrieving JSON data from a document database. I.E if you were using firebase you'd make your call server/datacharts/unique-driverid/. This would return all dateTime and values for that driver. Your class definition would just be;

{
    "driverId": [{
                "dateTime": "2016-09-14 00:14:20",
                "seconds": "60.50"
            },{
                "dateTime": "2016-09-14 00:15:20",
                "seconds": "57.23"
            },{ 
                "dateTime": "2016-09-14 00:18:20",
                "seconds": "61.230"
            },
            {
                "dateTime": "2016-09-14 00:23:20",
                "seconds": "55.875"
        }]
} 

As I'm only saving the data locally, I had to flatten my data structure.

{
   "dataPoints": [{
                "dateTime": "2016-09-14 00:14:20",
                "seconds": "60.50",
                 "driverId": "0000000009"
            },{
                "dateTime": "2016-09-14 00:15:20",
                "seconds": "57.23",
                 "driverId": "0000000009"
            }]
} 

Which on load of the JSON will have to loop through and build a List of "dateTime" and "seconds" objects for the "selected" driver. This is what I didn't want to have to due but is seems all that is possible.

\$\endgroup\$
1
  • \$\begingroup\$ This also becomes very easy using Linq. Where you can use the 'Where' method in linq to return all the search term in the list. Where returns an ienumerable but there is the build in ToList<T>() that you can chain on to the 'where' function. dataSerialized.availableData = allDataPoints.availableData.Where(s => s.driverId == SelectedTeam.CurrentTeam.driversLiscense ).ToList<DataPoints>(); \$\endgroup\$
    – mushcraft
    Oct 3, 2016 at 21:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .