Reputation: 23
For a personal project, I'm trying to deserialize a big Json array into a list of objects, however I repeatedly run into issues when following other guides. I have the following json file:
{
PokemonJSONList : [
{
"id": 1,
"region": "Kanto",
"name": "Bulbasaur",
"typeList": [ "Grass", "Poison" ]
},
{
"id": 2,
"region": "Kanto",
"name": "Ivysaur",
"typeList": [ "Grass", "Poison" ]
},
{
"id": 3,
"region": "Kanto",
"name": "Venusaur",
"typeList": [ "Grass", "Poison" ]
},
{
"id": 3,
"form": "f2",
"region": "Kalos",
"name": "Mega Venusaur",
"typeList": [ "Grass", "Poison" ]
},
{
"id": 3,
"form": "f3",
"region": "Galar",
"name": "G-Max Venusaur",
"typeList": [ "Grass", "Poison" ]
},
{
"id": 4,
"region": "Kanto",
"name": "Charmander",
"typeList": [ "Fire" ]
},
...
{
"id": 898,
"form": "f4",
"region": "Galar",
"Legend": "true",
"name": "Shadow Rider Calyrex",
"typeList": [ "Psychic", "Grass" ]
}
]
}
As you can see, some json entries have more key-value pairs than others, as this json file is used for more than just this project. I only need the Name, Region, Typelist, and Legend(not shown, as the json file goes on considerably longer) fields. I also have this class:
[System.Serializable]
public class PokemonList : MonoBehaviour
{
public List<PokemonOBJ> PokemonJSONList;
}
Meant to recieve this json data, and this class:
[System.Serializable]
public class PokemonOBJ : MonoBehaviour
{
public string Name { get; set; }
public string Region { get; set; }
public List<string> TypeList { get; set; }
public bool Legend { get; set; }
}
To be the base object for storing the data for each entry. However, when trying to serialize the json file as such:
string jsonString = @"Assets/JSON/data.json";
public PokemonList PokemonDB;
PokemonDB = JsonUtility.FromJson<PokemonList>(jsonString);
results in a "cannot deserialize JSON to new instances of type PokemonList" tutorials I used showed using PokemonOBJ for FromJson but that results an error in vs itself, and I'm a bit lost here on how to get this working properly, and what I'm doing wrong.
Upvotes: 2
Views: 2275
Reputation: 3382
From the docs:
Only plain classes and structures are supported; classes derived from UnityEngine.Object (such as MonoBehaviour or ScriptableObject) are not.
Upvotes: 1