-1

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!

George
  • 45
  • 7
  • Try https://json2csharp.com/ to def your output classes – J.Salas Jan 12 '22 at 12:21
  • 1
    @J.Salas Or the Visual Studio "Paste JSON as classes" option. – Cleptus Jan 12 '22 at 12:25
  • https://forum.unity.com/threads/how-to-read-json-file.401306/ that helped me. Thanks everyone. – George Jan 12 '22 at 12:26
  • Well to me `{"data":"Assets/Resources/GameJSONData/Page123.json"}` doesn't look like it will contain any fields at all ;) I guess you rather wanted to actually read the content of that file first .... – derHugo Jan 12 '22 at 13:09

1 Answers1

1

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);