Error ArgumentException: JSON must represent an object type from PyZeroMQ server

Viewed 209

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

[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!

1 Answers

The issue is in your Python code; namely, x is already a JSON string; you don't need to call json.dumps(x), you can send it direct using socket.send_string(x) instead.

In fact, y = json.dumps(x) returns a JSON-encoded JSON string!:

'"{\\"playerId\\":8484239823,\\"playerLoc\\":\\"Powai\\",\\"playerNick\\":\\"Random Nick\\"}"'

which cannot be decoded into a Player object - because it's a string! Remove that line in your Python code, and just socket.send_string(x) directly.

Related