1

I am working with Unity and Socket.IO and a NodeJS server when a player joins i need to send him the info abbout the other players to add them to his game. The server sends this json string:

{  
   "players":{  
      "/#3vd98L1t96vgMwRsAAAB":{  
         "name":"Player #2",
         "x":0,
         "y":0
      },
      "/#RaLyZw7Rnp94nDXkAAAF":{  
         "name":"Player #6",
         "x":0,
         "y":0
      }
   }
}

But i can't find out how to forloop this in unity? I thought making a object out of it would work so this is what i came up with:

JSONObject players = new JSONObject(e.data.GetField("players").ToString());

But how can i make a foreach loop out of it?

Menno van Hout
  • 39
  • 1
  • 3
  • 8
  • I am not getting the question here, can you give the sample output – Devank Apr 30 '16 at 17:34
  • @Devank i need a for each loop to insert all players so a for each loop that loops through: "/#3vd98L1t96vgMwRsAAAB" and through: "/#RaLyZw7Rnp94nDXkAAAF" The sample output is just the string as the example above: {"players":{"/#z91LtpNg70y3GiUDAAAA":{"name":"Player #1","x":0,"y":0}}} – Menno van Hout Apr 30 '16 at 17:37

2 Answers2

1

Assuming that your e.data is a JSONObject created from the JSON you show, you can do:

var players = e.data.GetField("players");

for(int i = 0; i < players.list.Count; i++)
{
    string playerKey = (string)players.keys[i];
    JSONObject playerData = (JSONObject)players.list[i];
    // Process the player key and data as you need.
}

For the relevant docs see JSONObject: Decoding.

For convenience, you might want to encapsulate this logic in an extension method for later re-use:

public static class JSONObjectExtensions
{
    public static IEnumerable<KeyValuePair<string, JSONObject>> NameValuePairs(this JSONObject obj)
    {
        if (obj == null)
            throw new ArgumentNullException();
        if (obj.type != JSONObject.Type.OBJECT)
            throw new InvalidOperationException(string.Format("Incoming object type {0} is not JSONObject.Type.OBJECT.", obj.type));
        for (int i = 0; i < obj.list.Count; i++)
        {
            string key = (string)obj.keys[i];
            JSONObject j = (JSONObject)obj.list[i];
            yield return new KeyValuePair<string, JSONObject>(key, j);
        }
    }
}

Then you can do

foreach (var pair in players.NameValuePairs())
{
    // Process the player key and data as you need.
}
dbc
  • 104,963
  • 20
  • 228
  • 340
0

You did not mention the library you are using. Unity added native Jason serialzer in version 5.3.

Convert Class to Json string:

string jsonString = JsonUtility.ToJson(className);

Convert Json string to class:

ClassName serializedPlayer = new ClassName();
serializedPlayer = JsonUtility.FromJson<ClassName>(jsonString);

The answer I provided here describes how to use array json. It's a long process to be reposted but it is what you are looking for. You should read the second solution that says "2. MULTIPLE DATA(ARRAY JSON)."

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328