Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

json deserialize in Unity C#

I’m new to JSON, and I’m trying to deserialize to a JSON array, but I’m getting an Invalid Value error.
Please explain to me what I am doing wrong. Thanks in advance.
Below I attach the source code and the JSON file.

using UnityEngine;

public class PageDefinitionTest : MonoBehaviour
{
public int pageNumber;
public void Start()
{
    string jsonString = 
 $"Assets/Resources/GameJSONData/Page{pageNumber}.json";
    PageDefinition def = JsonUtility.FromJson<PageDefinition>(" 
{\"data\":" + jsonString.ToString() + "}");
    Debug.Log(def);
}
}
[System.Serializable]
public class PageDefinition
{
public RectData[] data;
}

[System.Serializable]
public class RectData
{
public Rect area;
}

"areaDefinitions": [
{
  "area": {
    "serializedVersion": "2",
    "x": -3.5999999046325685,
    "y": 2.5143675804138185,
    "width": 7.199999809265137,
    "height": 1.9955297708511353
  },
  "type": 0
},
{
  "area": {
    "serializedVersion": "2",
    "x": -3.5999999046325685,
    "y": 0.21950837969779969,
    "width": 7.199999809265137,
    "height": 2.155172109603882
  },
  "type": 0
},
{
  "area": {
    "serializedVersion": "2",
    "x": -3.5999999046325685,
    "y": -1.9206972122192383,
    "width": 7.199999809265137,
    "height": 1.5465353727340699
  },
  "type": 0
},
{
  "area": {
    "serializedVersion": "2",
    "x": -3.5999999046325685,
    "y": -3.731640338897705,
    "width": 7.199999809265137,
    "height": 1.4966471195220948
  },
  "type": 0
},
{
  "area": {
    "serializedVersion": "2",
    "x": 3.5999999046325685,
    "y": 3.362467050552368,
    "width": 7.199999809265137,
    "height": 1.63633394241333
  },
  "type": 0
},
{
  "area": {
    "serializedVersion": "2",
    "x": 3.5999999046325685,
    "y": 1.097541332244873,
    "width": 7.199999809265137,
    "height": 2.3746800422668459
  },
  "type": 0
},
{
  "area": {
    "serializedVersion": "2",
    "x": 3.5999999046325685,
    "y": -1.5415465831756592,
    "width": 7.199999809265137,
    "height": 2.32479190826416
  },
  "type": 0
},
{
  "area": {
    "serializedVersion": "2",
    "x": 3.5999999046325685,
    "y": -3.7466068267822267,
    "width": 7.199999809265137,
    "height": 1.5465354919433594
  },
  "type": 0
}
]
}

I succeeded in JSON serialization, it’s simpler than it seems, but Deserialization doesn’t work out. Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you should put the text inside the json file for jsonString not the path to file.

for reading text files you have several options:

1- get from the inspector:

define a TextAsset in your class like this:

[SerializeField] TextAsset myfile;

you can also use Resources.Load(resourceRelativePath) for this

then read the jsonString like this:

var jsonString = textFile.text;

2- have the exact path to file:(read documentation about streamingAssets and PersistentDataPath:

        var jsonString = File.ReadAllText(filePath);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading