Reading this JSON file

Hey, I got this JSON file from someone, but I couldn’t find a solution with a JSON file formatted like this one that explains how I can put my JSON values into float arrays.

The JSON contains values for time, x, y, x-velocity, y-velocity

[
   [
      0.0,
      -0.0,
      1.0,
      0.0,
      5.0,
      0.0
   ],
   [
      0.01,
      0.00075,
      0.99999831334375,
      -0.25763589812764937,
      4.999949451675574,
      -0.022482897703140683
   ],
   [
      0.02,
      0.0019999873629188937,
      0.9999880161513413,
      -0.6861371272100371,
      4.999641481646385,
      -0.05987532881528388
   ],
   [
      0.03,
      0.003749861881495129,
      0.9999579210647058,
      -1.2840585639027504,
      4.998744414678499,
      -0.11204586882393658
   ],
   [
      0.04,
      0.005999296868100454,
      0.9998924571594066,
      -2.049159211875057,
      4.996802578753328,
      -0.1787847559611664
   ],
   [
      0.05,
      0.008747538286414783,
      0.9997717804350016,
      -2.978191605321729,
      4.993246914800202,
      -0.259779229035477
   ],
   [
      0.06,
      0.011993148781034916,
      0.9995719432308737,
      -4.066651219867101,
      4.987411143879327,
      -0.35458437910927293
   ],
   [
      0.07,
      0.01573370713894441,
      0.9992651411229377,
      -5.308506740020048,
      4.978554864457109,
      -0.46259211146586565
   ],
   [
      0.08,
      0.019965478773732954,
      0.9988200562639874,
      -6.695939255501838,
      4.965894579457251,
      -0.583001737319105
   ],
   [
      0.09,
      0.02468307862421734,
      0.998202313435944,
      -8.219125122248375,
      4.948642833751612,
      -0.7147965472488054
   ],
   [
      0.1,
      0.029879153599656535,
      0.9973750585951063,
      -9.866101428531966,
      4.926054370787294,
      -0.8567311924094966
   ]
]

I’m really not sure but can I use this as is or do I need to make changes/reformat the JSON file?
Haven’t worked with JSON files yet, just thought it would be a good solution for what I wanted to do.

Thanks in advance!

JSON is great! But here are some things to know about JSON, specifically when used with Unity3D:

  1. The built-in Unity3D JSON processor is VERY limited. It is intended for fixed-shape structures being serialized and deserizalized. It does NOT work with List<> or Dictionary<> or other generic collections. Its intent is performance, not versatility.

  2. I highly recommend getting the JSON.NET asset from the Asset Store, because that is much more robust.

Specifically about the data you show above, it looks to be an array of arrays. If you deserialize it with JSON.net, you will need a class to stick it into.

There are sites like http://json2csharp.com that can look at your JSON and make you that correctly-shaped class. It’s pretty awesome.

There’s also jsonlint.com, which is for verifying JSON to make sure it is syntactically correct.

Hop right in… JSON is great.

Thank you! So I need to read lists! The syntax seems to be right(according to jsonlint.com), but it won’t let me convert it on json2csharp.com
It says it’s not valid…Is there a tutorial explaining how JSON.NET and setting up this class works? I can only find the ones parsing JSON objects but not Lists.

And wouldn’t it be easier to reformat my JSON to objects for each variable?

I see the problem. I don’t know JSON jargon, but most JSON is in the form of dictionary key pairs, like so:

{ "name": "Kurt", "online": 1}

The data you provided is just a raw array of arrays. Didn’t even know that was valid, but here’s what I did to make it generate C# classes, since obviously a variable has to have a name. (try putting the above snippet into C# maker, you’ll see).

I basically added an open-brace “data”: at the start and a close brace at end, so your data becomes:

{
    "data": [
        [
            0.0,
            -0.0,
            1.0,
            0.0,
            5.0,
            0.0
        ],
        [
            0.01,
            0.00075,
            0.99999831334375,
            -0.25763589812764937,
            4.999949451675574,
            -0.022482897703140683
        ],
        [
            0.02,
            0.0019999873629188937,
            0.9999880161513413,
            -0.6861371272100371,
            4.999641481646385,
            -0.05987532881528388
        ],
        [
            0.03,
            0.003749861881495129,
            0.9999579210647058,
            -1.2840585639027504,
            4.998744414678499,
            -0.11204586882393658
        ],
        [
            0.04,
            0.005999296868100454,
            0.9998924571594066,
            -2.049159211875057,
            4.996802578753328,
            -0.1787847559611664
        ],
        [
            0.05,
            0.008747538286414783,
            0.9997717804350016,
            -2.978191605321729,
            4.993246914800202,
            -0.259779229035477
        ],
        [
            0.06,
            0.011993148781034916,
            0.9995719432308737,
            -4.066651219867101,
            4.987411143879327,
            -0.35458437910927293
        ],
        [
            0.07,
            0.01573370713894441,
            0.9992651411229377,
            -5.308506740020048,
            4.978554864457109,
            -0.46259211146586565
        ],
        [
            0.08,
            0.019965478773732954,
            0.9988200562639874,
            -6.695939255501838,
            4.965894579457251,
            -0.583001737319105
        ],
        [
            0.09,
            0.02468307862421734,
            0.998202313435944,
            -8.219125122248375,
            4.948642833751612,
            -0.7147965472488054
        ],
        [
            0.1,
            0.029879153599656535,
            0.9973750585951063,
            -9.866101428531966,
            4.926054370787294,
            -0.8567311924094966
        ]
    ]
}

You don’t need to change your data, you can just prepend and post-pend that before you blast it into json2csharp, then do it again right before you hydrate (another term for deserialize) it in Unity.

This is the class that json2csharp makes:

public class RootObject
{
   public List<List<double>> data { get; set; }
}

If you deserialize it with that type it should work. I forget the API and don’t have it in front of me but, it would be like:

var blob = DeserializationFunction<RootObject>( TheJSONString);

That would return you a RootObject (call it whatever you want) with all your data.

That’d be JsonUtility.FromJson<RootObject>( TheJSONString ); :wink:

1 Like