I am creating a game in the Unity game engine and while trying to load some JSON into some objects, I have encountered an issue. When I deserialize the JSON object, one of the object's fields, Attributes
, which is a list of Character_Attribute
objects, is being filled with null
. The number of nulls is correct but it isn't loading the data correctly.
This is an example of the JSON that I am trying to parse:
{
"Name":"Test Character 1",
"Location_ID":1,
"Attributes":[
{"Attribute_Type":0,"Base_Value":10,"Current_Value":10},
{"Attribute_Type":1,"Base_Value":7,"Current_Value":7},
{"Attribute_Type":2,"Base_Value":5,"Current_Value":5}
]
}
And these are the classes I am trying to load it into:
public class Character_Data
{
public string Name;
public int Location_ID;
public List<Character_Attribute> Attributes;
}
[Serializable]
public class Character_Attribute : ScriptableObject
{
public ATTRIBUTES Attribute_Type;
public int Base_Value;
public int Current_Value;
}
ATTRIBUTES
is a simple enum.
This is the code I am using to deserialize the JSON, where json_string is the JSON from above:
Character_Data data = JsonUtility.FromJson<Character_Data>(json_string);
All of the elements of data.Attributes
are null
. All other properties are loading fine. Is this happening because Character_Attribute
is a ScriptableObject
? If so, why is that and is there any way to get around this or am I going to have to make an intermediary class to load the JSON into and then move all that data into Character_Attribute
afterwards?
CodePudding user response:
I created the classes and tried to deserialize the json string to Character_Data
object
public class Character_Data
{
public string Name;
public int Location_ID;
public List<Character_Attribute> Attributes;
}
[Serializable]
public class Character_Attribute : ScriptableObject
{
public ATTRIBUTES Attribute_Type;
public int Base_Value;
public int Current_Value;
public override string ClassName => string.Empty;
}
public enum ATTRIBUTES
{
dev = 1,
test = 2,
stage = 3
}
As you mentioned ATTRIBUTES is an enum. I had to add my own enum values.
To implement the ScriptableObject
, I installed ScriptableObjectFramework
Nuget package.
However the difference I see in my implementation is, while implementing ScriptableObjectFramework
abstract class, the Character_Attribute
class has to implement an override
property i.e.
public override string ClassName => string.Empty;
(I assigned string.Empty to ClassName).
You can use Newtonsoft.Json
Nuget package to serialization or deserialization.
Below is my sample implementation:
var _object = new Character_Data
{
Name = "Test Character 1",
Location_ID = 1,
Attributes = new List<Character_Attribute>
{
new Character_Attribute { Attribute_Type = ATTRIBUTES.dev, Base_Value = 10, Current_Value = 10 },
new Character_Attribute { Attribute_Type = ATTRIBUTES.test, Base_Value = 7, Current_Value = 7 },
new Character_Attribute { Attribute_Type = ATTRIBUTES.stage, Base_Value = 5, Current_Value = 5 }
}
};
var jsonString = JsonConvert.SerializeObject(_object);
Character_Data data = JsonConvert.DeserializeObject<Character_Data>(jsonString);
Namespaces I had to use:
using EcmaScript.NET;
using Newtonsoft.Json;
CodePudding user response:
add serialize to main class too, and change enum to int
[Serializable]
public class Character_Data
{
public string Name;
public int Location_ID;
public List<Character_Attribute> Attributes;
}
[Serializable]
public class Character_Attribute : ScriptableObject
{
public int Attribute_Type;
public int Base_Value;
public int Current_Value;
}
if it is still not working properly install Newtonsoft.Json-for-Unity https://github.com/jilleJr/Newtonsoft.Json-for-Unity#readme and use it