Search code examples
c#jsonfacebook-graph-apiunity-game-enginefacebook-unity-sdk

Facebook.MiniJSON Deserialize comments and nested values under from returning no values


We are using a FB.API call to retrieve the comments on a post, we are unable to retrieve the data nested under "from".

API Explorer command: "10153539873517828/comments?"

C# code

 FB.API("/10153539873517828/comments?", HttpMethod.GET, delegate (IGraphResult result)
                {
             string jsonMessage = result.RawResult;
             Debug.Log(jsonMessage);
             var soap = Json.Deserialize(jsonMessage) as Dictionary<string,object>;
             List<object> entries = soap["data"] as List<object>;

             try
             {
                 for (int i = 0; i < soap.Count; i++)
                 {
                     Dictionary<string,object> messageData = entries[i] as Dictionary<string,object>;
                     object resultData = messageData["message"];             (Works)
                     object resultData = messageData["name"];                  (Blank)
                     Debug.Log("JSON string : " + resultData.ToString());
                 }
             }
             catch
             {
                 Debug.Log("Done!!!");
             }
         });

JSON string

{
   "data": [
     {
       "created_time": "2016-03-31T11:10:18+0000",
       "from": {
         "name": "Hello world",
         "id": "298062736962722"
       },
       "message": "Test comment",
       "id": "10153539873517828_10153539875377828"
     },
     {
       "created_time": "2016-03-31T11:12:47+0000",
       "from": {
         "name": "Hello world",
         "id": "298062736962722"
       },
       "message": "people must be wonder what the bleep is this, lol.",
       "id": "10153539873517828_10153539877202828"
     }
   ],
   "paging": {
     "cursors": {
       "before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVEF4TlRNMU16azROelV6TnpjNE1qZAzZANVFExT1RReU1qWXhPQT09",
       "after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZANVEF4TlRNMU16azROemN5TURJNE1qZAzZANVFExT1RReU1qYzJOdz09"
     }
   }
 }

We can successfully get the "message" and "id" under "data" when trying to get the "name" and the associated "id" it returns blank values.

We have read many post and had a look at some of the solution people have posted and we have not found anything that has helped to resolve the issue.

As we are using the FB SDK it would stand to reason that we should be able to use the Facebook.MiniJSON to deserialize the above string. Retrieving:

      {
    "created_time": "2016-03-31T11:10:18+0000",
    "from": {
      "name": "Hello world",
      "id": "298062736962722"
    },
    "message": "Test comment",
    "id": "10153539873517828_10153539875377828"
  },

All the example on the developer documentation only cover first level retrieval, how can we fix this?


Solution

  • name doesn't exist in the context you are searching. The only keys that are available are created_time, from, message, id. You have to create a dictionary out of from and reference the name from that. Here is the change I would suggest. This is not tested and I just grabbed your inner for loop and modified it. Obviously integrate back into the rest of your code.

    ...
    for (int i = 0; i < soap.Count; i++)
    {
        Dictionary<string,object> messageData = entries[i] as Dictionary<string,object>;
        object resultData = messageData["message"];
        Dictionary<string,object> fromData = messageData["from"] as Dictionary<string, object>;
        object resultData = fromData["name"]
        Debug.Log("JSON string : " + resultData.ToString());
    }
    ...