Issue reading JSON using LitJSON

Hi all,

Tearing my hair out here, trying to read level data from a JSON file.

If I write: Debug.Log("Rotation: " + ItemData["Level1"][HoldFloor]["Rotation"]["y"]);
it works and prints 30.0 in the console. Cool.

If I write:

        float DemCheck = ItemData["Level1"][HoldFloor]["Rotation"]["y"];
        Debug.Log("Rotation: " + DemCheck );

I get an error in VS which says "Cannot implicitly convert type ‘LitJson.JsonData’ to 'float.

So, then, I try a cast:

        float DemCheck = (float)ItemData["Level1"][HoldFloor]["Rotation"]["y"];
        Debug.Log("Rotation: " + DemCheck );

which doesn’t give an error in VS, but in Unity I get “InvalidCastException: Instance of JsonData doesn’t hold an int”.

I am at a loss. So two questions: How do I solve this? And what it the best JSON library for Unity?
I am using Unity 5.6, LitJson 0,9.0 and VS 2017 Community.

Any ideas are greatly appreciated.

Try this. I don’t use litjson, I use json.net, but I think they are similar.

float DemCheck = float.Parse(ItemData["Level1"][HoldFloor]["Rotation"]["y"].ToString());
        Debug.Log("Rotation: " + DemCheck );

The data type of this:

ItemData["Level1"][HoldFloor]["Rotation"]["y"]

Is a JsonData, which for some reason I don’t really get, can’t be typecast directly into float. It can be typecast into double, which can be typecast into float… as silly as that is.

float DemCheck = (float)(double)ItemData["Level1"][HoldFloor]["Rotation"]["y"]
2 Likes

That works! Insane that it should have to be done that way. But my deep thanks @StarManta