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

Unity Facebook SDK, can't retrieve Country from Dictionary after Login


I am retrieving the country of a facebook user as following:

 void Get()
    {
        FB.API("/me?fields=location{location{country}}", HttpMethod.GET,this.callback1);
    }

    private void callback1(IResult result)
    {
        if (result.Error != null)
        {
            Debug.Log("FB.API result = null");
        }
        else
        {
            Dictionary<string, object> dict = Facebook.MiniJSON.Json.Deserialize(result.RawResult) as Dictionary<string, object>;
    }
}

This works fine.

JSON looks like:

{
  "location": {
    "location": {
      "country": "England"
    },
    "id": "111122233344"
  },
  "id": "1112299000"
}

Problem is that the dict result is like nested dictionary, i can't find straightforward way to retrieve the country name.


Solution

  • I cannot test this end to end, drawing from the docs it should look like this:

    var dict = Json.Deserialize(jsonString) as Dictionary<string,object>;
    
    object locationH;
    string country;
    if(dict.TryGetValue ("location", out locationH)) {
      var location = (((Dictionary<string, object>)locationH) ["location"]);
      var countryDict = (((Dictionary<string, object>)location) ["country"]);
      country = (string)countryDict;
    }