Having a problem trying to parse a Json object returned from my Python server.
The error I am getting is
“ArgumentException: JSON must represent an object type.”
The puzzling thing is if I take the exact string returned from the server and hard code it before passing it to the parsing method Player.CreateFromJSON it works!
These are the 3 relevant lines in the Python server
x = '{"playerId":8484239823,"playerLoc":"Powai","playerNick":"Random Nick"}'
y = json.dumps(x)
socket.send_string(y)
This is the JSON string returned.
"{\"playerId\":8484239823,\"playerLoc\":\"Powai\",\"playerNick\":\"Random Nick\"}"
This is my Player object taken from the documentation Unity - Scripting API: JsonUtility.FromJson
[System.Serializable]
public class Player
{
public string playerId;
public string playerLoc;
public string playerNick;
public static Player CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<Player>(jsonString);
}
}
Am using Unity’s native JsonUtility FromJson method as can be seen from above and I simply call
Player playerInfo = Player.CreateFromJSON(jsonStringFromServer);
to populate the Player object
Have spent countless hours googling and trying to find similar issues. Any feedback or troubleshooting steps welcome!