Hello !
I am using the Facebook MiniJSON feature to loop inside a JSON from the Graph API provided by Facebook.
Everything worked fine until now (I managed to get all the JSON infos, put it in a list so I can extract each entry independently by turning them to strings, etc…).
BUT : sometimes some entries are missing (for exemple, when I look into the Feed to get all the posts from a user, some posts don’t have a “message” key. So, my loop stops when there is no key, and I can’t access the next keys.
SO, I would like to know if there is a way to put a condition on it : “when there is no such key, ignore it and go to the next entry”.
I hope you will understand what I’m trying to do… Here is my code (in C#) :
void FBFeedInfos(FBResult result)
{
var JSONresult = Json.Deserialize(result.Text) as Dictionary<string, object>;
object info;
var feedList = new List<object>();
Debug.Log("FEED : " + result.Text);
if(JSONresult.TryGetValue ("feed", out info))
{
feedList = (List<object>)(((Dictionary<string, object>)info) ["data"]);
if(feedList.Count > 0)
{
for (int i = 0; i < feedList.Count; i++)
{
// Get the input "message" for each entry
string message = System.Convert.ToString(((Dictionary<string, object>)feedList[i])["message"]);
Debug.Log(message);
// Put text result on the UI
GameObject FeedPanel;
FeedPanel = Instantiate (FeedEntry) as GameObject;
FeedPanel.transform.SetParent(FeedScrollList.transform);
Transform ThisFeedMessage = FeedPanel.transform.Find ("FeedMessage");
Text FeedMessage = ThisFeedMessage.GetComponent<Text>();
FeedMessage.text = message;
}
}
else
{
Debug.Log("Failed to get feed!");
}
}
So I get the data on the Feed, with the “message” key. But when there is no such key, I got an error on line 18 : “KeyNotFoundException: The given key was not present in the dictionary”.
I really hope you will be able to help me with that.
Thank you in advance !
EDIT : Just FYI, the “result” is a JSON string that I get when I call the Facebook Graph API.