I have a JSON like this:
{
"userName" : "player3322",
"userLevel" : 23,
"userInventory" : {
"primaryHand" : "3493",
"secondaryHand" : "none"
}
}
to deserialize this I've made two classes like this:
public class PlayerData{
public string userName;
public int userLevel;
public UserInventoryData userInventory;
}
public class UserInventoryData {
public string primaryHand,secondaryHand;
}
But for some reason after deserializing it using JsonUtility.FromJson , userInventory inside the playerData class stays null and I can't access to the content inside userInventory. How can I deserialize all of the my JSON? thanks
CodePudding user response:
I'm not sure what deserializer you are using, but with Newtonsoft.Json
, this is working fine.
using Newtonsoft.Json;
string str = "{'userName' : 'player3322','userLevel' : 23,'userInventory' : {'primaryHand' : '3493','secondaryHand' : 'none',}}";
var obj = JsonConvert.DeserializeObject<PlayerData>(str);
Console.WriteLine(obj);
Console.WriteLine(obj.userInventory.primaryHand);
public class PlayerData{
public string userName;
public int userLevel;
public UserInventoryData userInventory;
}
public class UserInventoryData {
public string primaryHand,secondaryHand;
}