0

I am trying to build Multiplayer game where Java is my server and Unity is my Clinet. I am able to send data to server and read data in Json format in the server(Java Server) But when i try to read the response i am facing issues. I am unable to map properly the Json response to the Classes which are creatied in Unity.

Please find below.

public class Player
{
    public string playerID;
    public string name;
    public string playerPosX;
    public string playerPosY;
    public string playerPosZ;
}

public class Lobby 
{

    public string lobbyID;

    public ArrayList player;
}


lobby = JsonUtility.FromJson<Lobby>(response);
Debug.Log(lobby.lobbyID);
Debug.Log(lobby.player.Count);

I am getting the json data as

{"player":[{"playerID":"P1","name":"","playerPosX":"","playerPosY":"","playerPosZ":"","myne":false}],
"lobbyID":"L1"
}

and i am getting following error. at 2nd log Object reference not set to an instance of an object

2 Answers 2

1

Not sure if you're locked into using the Unity Json utility, but I had success with your Json string using the Newtonsoft.Json nuget library:

using Newtonsoft.Json;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            string json = "{\"player\":[{\"playerID\":\"P1\",\"name\":\"\",\"playerPosX\":\"\",\"playerPosY\":\"\",\"playerPosZ\":\"\",\"myne\":false}],\"lobbyID\":\"L1\"}";
            Lobby lobby = JsonConvert.DeserializeObject<Lobby>(json);
            System.Diagnostics.Debug.WriteLine(lobby.lobbyID);
            System.Diagnostics.Debug.WriteLine(lobby.player.Length);
        }
    }

    public class Lobby
    {
        public Player[] player { get; set; }
        public string lobbyID { get; set; }
    }

    public class Player
    {
        public string playerID { get; set; }
        public string name { get; set; }
        public string playerPosX { get; set; }
        public string playerPosY { get; set; }
        public string playerPosZ { get; set; }
        public bool myne { get; set; }
    }
}

Output is:

L1
1

Which is what I gather you're looking for.

Sign up to request clarification or add additional context in comments.

3 Comments

If i use get and set its not working even Lobby ID is coming as null
I can see there is difference you are using Newtonsoft.Json but i am using UnityEngine.JsonUtility
Thanks mate working ! added these new libraries and it started working. The default Json parcer in Unity is not working as expected.
0

This structure should work, deserialize to type Lobby:

public class Player
{
    public string playerID;
    public string name;
    public string playerPosX;
    public string playerPosY;
    public string playerPosZ;
    public bool myne;
}

public class Lobby
{
    public List<Player> player;
    public string lobbyID;
}

2 Comments

Not working may be my Json api need to be changed i guess
Yeah Newtonsoft.Json is the better library, faster, less strict, well tested and Microsoft supported. Then just use JsonConvert class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.