- Home /
Parse Json Response through Dictionary object
I have Json response through Facebook API like this:
Now I want to parse this data so I can use within my game. For this purpose, I have written code up to this:
public void OnChallengesButtonClick ()
{
SoundManager.Instance.PlayButtonClickSound ();
FB.API ("/me/friends", HttpMethod.GET, RetrieveFacebookFriends);
}
public void RetrieveFacebookFriends (IGraphResult result)
{
if (!string.IsNullOrEmpty (result.Error)) {
Debug.Log ("ERROR: " + result.Error);
return;
}
IDictionary<string,object> friendsDict = result.ResultDictionary;
Debug.Log ("raw: " + result.RawResult);
}
So how to extract Name and ID data from available Json response through Dictionary object? Please give me some help into this.
Answer by Bunny83 · Jul 31, 2018 at 12:18 PM
Well you use a third party framework and probably haven't took the time reading through their documentation, right? They have an example over here.
You can use their Dictionary / List result if you want though you have to cast the "object" to the right type. I guess the ResultDictionary represents the top most object in the JSON structure. So to access the List you would do
var dataList = (IList<object>)result.ResultDictionary["data"];
Now you can iterate through the objects like this:
foreach(var obj in dataList)
{
var friend = (IDictionary<string, object>)obj;
string name = (string)friend["name"];
string id = (string)friend["id"];
// do something
}
This is just an assumption as i did not read the whole documentation.
Since they also provide the "RawResult" you could of course use a different Json parser. For example with my SimpleJSON you could just do
var data = JSON.Parse(result.RawResult);
foreach(var friend in data["data"])
{
string name = friend["name"];
string id = friend["id"];
// do something
}
@Bunny83 Awesome answer, really thanks for your reply sir :)
Hi i have same error am facing can you please give the suggesation?
Am getting this error on ["data"] Please solve this issue. $$anonymous$$eyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[System.String,System.Object].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
Your answer
Follow this Question
Related Questions
Parsing Facebook graph JSON 1 Answer
forcing unload a file after error 0 Answers
Looking for Code that will Read JSON files (C# code) 2 Answers
How to parse Json file in android? 3 Answers