Facebook deserializing
Hey, I need help deserializing the JSON i get back from facebook.
I've been trying numerous ways to parse it but no success. The only thing i seem to be parsing is the number of friends who have highscores, which is 2 : http://screencast.com/t/mbtTRexUBp0m
The issue comes when I try to parse the name and score of the people in the json.
InvalidCastException: Cannot cast from source type to destination type.
I/Unity (21869): at FacebookScript.GETCallback (IGraphResult result) [0x00000] in <filename unknown>:0
I/Unity (21869): at Facebook.Unity.AsyncRequestString+<Start>c__Iterator1.MoveNext () [0x00000] in <filename unknown>:0
The raw result which I recieve (seen from logcat):
Raw:{"data":[{"score":60,"user":{"name":"JOHNY JOHN","id":"0000000000000"}},{"score":50,"user":{"name":"JOHN JOHN","id":"0000000000000"}}]}
Here is my code:
public void GETCallback(IGraphResult result)
{
if (result.ResultDictionary != null)
{
Debug.Log("Raw:" + result.RawResult);
var dict = Json.Deserialize(result.RawResult) as Dictionary<string, object>;
var friendList = new List<object>();
friendList = (List<object>)(dict["data"]);
int _friendCount = friendList.Count;
Debug.Log("Items found:" + _friendCount);
List<string> friendIDsFromFB = new List<string>();
/*for (int i = 0; i < _friendCount; i++) // Tried this, same error.
{
foreach(KeyValuePair<string, object> entry in friendList)
{
Debug.Log(entry.Key + "|" + entry.Value);
}
string friendFBID = getDataValueForKey((Dictionary<string, object>)(friendList[i]), "id");
string friendName = getDataValueForKey((Dictionary<string, object>)(friendList[i]), "name");
Debug.Log(i + "/" + _friendCount + "|" + friendFBID +"|"+ friendName);
NPBinding.UI.ShowToast(i + "/" + _friendCount + "|" + friendFBID + "|" + friendName, VoxelBusters.NativePlugins.eToastMessageLength.LONG);
//friendIDsFromFB.Add(friendFBID);
}*/
foreach(KeyValuePair<string, object> entry in friendList) // Tried this, same error.
{
Debug.Log(entry.Key + "|" + entry.Value);
}
}
else
{
NPBinding.UI.ShowToast("result.ResultDictionary is null", VoxelBusters.NativePlugins.eToastMessageLength.LONG);
}
}
private string getDataValueForKey(Dictionary<string, object> dict, string key)
{
object objectForKey;
if (dict.TryGetValue(key, out objectForKey))
{
return (string)objectForKey;
}
else {
return "";
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612073325im_/https://answers.unity.com/themes/thub/images/avi.jpg)