Search code examples
c#jsonfacebookunity-game-enginefacebook-unity-sdk

How to parse a JSON with multiple id's given by FB.API call


EDIT: I "solved" it by not parsing anything and "reading" it by hand like so:

foreach (string key in result.ResultDictionary.Keys)
{
    foreach (string key2 in ((IDictionary)result.ResultDictionary[key]).Keys)
    {
        Debug.Log(key2 + " : " + ((IDictionary)result.ResultDictionary[key])[key2]);
    }
}

Original question below

I have this snippet:

string query = "?ids=";
for (int i = 0; i < users.Length; i++)
{
    query += users[i].idFacebook + ",";
}
query = query .Remove(query.Length - 1);
FB.API(query, HttpMethod.GET, responseCallback);

Now in the callback I want to parse the JSON I got, but I don't know how to do it, it looks something like this:

{
    "facebook-id-1":
    {
        "name":"some name",
        "id":"facebook-id-1"
    },
    "facebook-id-2":
    {
        "name":"some name",
        "id":"facebook-id-2"
    },
    "facebook-id-3":
    {
        "name":"some name",
        "id":"facebook-id-3"
    },
}

I tried to use a struct to parse it, but I know I need another struct to read the "upper" nodes, but their names are the id's so I can't create a general struct to work with them

struct fbResults
{
    public fbName[] users;
}

struct fbName
{
    public string name;
    public string id;
}

This is how I parse the JSON inside the callback:

fbResults fbUsers = JsonUtility.FromJson<fbResults>(result.RawResult);

I appreciate any help

EDIT: Added more tags.


Solution

  • I "solved" it by not parsing anything and "reading" it by hand like so:

    foreach (string key in result.ResultDictionary.Keys)
    {
        foreach (string key2 in ((IDictionary)result.ResultDictionary[key]).Keys)
        {
            Debug.Log(key2 + " : " + ((IDictionary)result.ResultDictionary[key])[key2]);
        }
    }