I'm trying to use Json to hold game dialog in a text asset file. I've tried to make a barebones project to test this and it is failing with the Error Message: ArgumentException: JSON must represent an object type.
I have a class called LineData which has a few properties each line of dialog contains. In the Json there are just two lines for now. When I import and parse it to try and make a reference to one of the lines of dialog it fails.
Here is all the code:
dialogJson.JSON:
[
{
"lineID": "BEDROOM_DAVE_0001",
"lineDialog": "Yaawwn, up i get for another fun packed day.",
"lineDuration": 3
},
{
"lineID": "BEDROOM_DAVE_0002",
"lineDialog": "Well I think I need a cigarette before I get to work!",
"lineDuration": 3
}
]
LineData.cs
using System;
[Serializable]
public class LineData
{
public string lineID;
public string lineDialog;
public float lineDuration;
}
GameController.cs
using UnityEngine;
public class GameController : MonoBehaviour
{
TextAsset Dialog_Data;
private void Start()
{
Dialog_Data = Resources.Load<TextAsset>("txt/dialogJson");
LineData line1 = JsonUtility.FromJson<LineData>(Dialog_Data.text);
Debug.Log("line 1 dialog = " + line1.lineDialog);
}
}
NOTE: I read here (https://answers.unity.com/questions/1503047/json-must-represent-an-object-type.html) that Unity "can't parse if the json consists of an array of objects". But I don't understand his explanation on how to get around this. Thanks
EDIT: I now have it without error, but the debug message is just empty didnt seem to get any data) . Here is the json now after the edits:
{ "LineDatas":
[
{
"lineID": "BEDROOM_DAVE_0001",
"lineDialog": "Yaawwn, up i get for another fun packed day.",
"lineDuration": 3
},
{
"lineID": "BEDROOM_DAVE_0002",
"lineDialog": "Well I think I need a cigarette before I get to work!",
"lineDuration": 3
}
]
}