I’m trying to use JsonUtility to deserialize a list of objects but I keep getting this error:
JSON must represent an object type.
I’ve tries using LitJson.JsonMapper as well, the error I get then is:
The value passed in must be an enum base or an underlying type for an enum, such as an Int32.\r\nParameter name: value
What am I doing wrong?
The code:
var stateDetails = new List<StateDetails>();
stateDetails = JsonUtility.FromJson<List<StateDetails>>(jsonString);
[Serializable]
public class StateDetails
{
public string Text { get; set; }
public States State { get; set; }
public List<Transition> Transitions { get; set; }
}
[Serializable]
public class Transition
{
public KeyCode KeyCode { get; set; }
public States State { get; set; }
}
[Serializable]
public enum States
{
room_0,
room_1,
room_2
};
jsonString:
[
{
"Text": "Sample text.",
"State": 0,
"Transitions":
[
{"KeyCode": "S", "State": 2},
{"KeyCode": "M", "State": 1}
]
},
{
"Text": "Sample text.",
"State": 2,
"Transitions":
[
{"KeyCode": "R", "State" : 0}
]
}
]