- Home /
Question by
Arisstephenson · Dec 30, 2018 at 10:21 AM ·
serializationjsonserializewrapper
Json class will serialize but wont deserialize
I made a simple wrapper class for an array to be serialized into json.
[System.Serializable] public class ItemWrapper { public Item[] items; }
Then I serialize and deserialize the class. Why do I only get an error (JSON parse error: Invalid value) when I try reading it?
ItemWrapper wrapper = new ItemWrapper();
Item[] oldItems = new Item[0];
oldItems = JsonUtility.FromJson<ItemWrapper>("Assets/items.json").items; //This is where I get the error
wrapper.items = oldItems.Union(new Item[] { newItem }).ToArray();
File.WriteAllText("Assets/items.json", JsonUtility.ToJson(wrapper, true));
I also made sure that the Item class is serializable.
Comment
Best Answer
Answer by Tobychappell · Dec 30, 2018 at 12:08 PM
The JsonUtility.FromJson method is expecting a json string, you have given it a path to a json file,
Read the contents of the file into a string and then pass that string into that method.