Reputation: 11
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 :
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 "";
}
}
Upvotes: 0
Views: 528
Reputation: 2516
I'm assuming that you're using MiniJSON (at least the version that used to come with the FB SDK)
N.B. Not tested for typos. Typing straight here in SO
var dict = Json.Deserialize(result.RawResult) as Dictionary<string, object>;
var datas = (List<object>)dict["data"];
foreach(var iterator in datas) {
var data = iterator as Dictionary<string, object>;
Debug.Log("Score is :: "+data["score"]);
//var score = int.Parse((string)data["score"]); //Parse to int after casting to string if you want the value
var userData = data["user"] as Dictionary<string, object>;
Debug.Log("Name is :: "+userData["name"]);
Debug.Log("ID is :: "+userData["id"]);
//var name = (string)userData["name"]; //Get the name
//var id = (string)userData["id"]; //...and the ID
}
Upvotes: 3