2

I am using Unity and the Facebook SDK.

Currently I am retrieving a list of users friends who have the app installed using the graph API:

The data when it is returned looks as such:

{
"data": [
{
  "installed": true,
  "id": "1292282928282"
},
{
  "installed": true,
  "id": "29282829292"
}
],
"paging": {
"next": "https://graph.facebook.com/v2.5/105157539862931/friends?fields=installed&format=json&access_token=CAALVPHznNpcBAOnO94HvqUgYKI2kObPZBgR0sqIOMSRO9swZBBTWHb6FjliZCT1KyCmPbnX42xvtngboh3DjFOrixw0pSenwRZA1oXZAHNDdYcGsHNOHjQcZB0f6fsZBQJjhOTttwQu7E5hZBDcAWJVZBGK2AxrZBDZBxLL7I5pjXwwbb12hDytZAiVzUmNzi1Ae2CCvOnL6QCpqzsJT7fWWjXXi&limit=25&offset=25&__after_id=enc_AdB7PJbXYkDSSZAq33AjPXZAeRnlrZBDwjAAILZAg3emHdei0qdRLa2AeD6sRuX6h0OQuPQi8x8bvSHPy0EqIgybYL89"
},
"summary": {
"total_count": 2
}
}

Currently I am trying to figure out how I can extract the "Installed" and id from each object inside of the "data" object.

so far I am doing this:

Dictionary<object,object> friendsList = Json.Deserialize (result.RawResult) as Dictionary<object,object>;

But I am not sure how I can pull out these specific items from the objects.

Does anyone know how I would go about doing this?

Backs
  • 24,430
  • 5
  • 58
  • 85
coder4life22
  • 199
  • 1
  • 2
  • 9
  • Have a look at [this link](http://stackoverflow.com/questions/34573186/json-from-facebook-and-unity?noredirect=1#comment56910827_34573186) – Hamza Hasan Jan 04 '16 at 07:43
  • Yes, but the data set JSON is different in this question – coder4life22 Jan 04 '16 at 07:55
  • You will get a `Dictionary` against `data`. Of course through `object`, then parse that `object(Dictionary)` in `Dictionary`. For self digging, first get whatever the variable is through `var` then print its `Type` then proceed accordingly. – Hamza Hasan Jan 04 '16 at 08:02

3 Answers3

1

You need to have classes that will hold the deserialized data for you. For example create the following classes:

public class Datum
{
    public bool installed { get; set; }
    public string id { get; set; }
}

public class Paging
{
    public string next { get; set; }
}

public class Summary
{
    public int total_count { get; set; }
}

public class RootObject
{
    public List<Datum> data { get; set; }
    public Paging paging { get; set; }
    public Summary summary { get; set; }
}

RootObject is the root class so the deserialization code will be:

var deserialized = Json.Deserialize<RootObject>(result.RawResult);

There is one more way to do it without creating classes, by using the dynamic keyword:

static void Main(string[] args)
    {

      var stringVal = @"{
    ""data"": [
    {
  ""installed"": true,
      ""id"": ""1292282928282""
},
{
    ""installed"": true,
  ""id"": ""29282829292""
}
],
""paging"": {
    ""next"": ""https://graph.facebook.com/v2.5/105157539862931/friends?        fields=installed&format=json&access_token=CAALVPHznNpcBAOnO94HvqUgYKI2kObPZBgR0sqIOMSRO9swZBBTWHb6FjliZCT1KyCmPbnX42xvtngboh3DjFOrixw0pSenwRZA1oXZAHNDdYcGsHNOHjQcZB0f6fsZBQJjhOTttwQu7E5hZBDcAWJVZBGK2AxrZBDZBxLL7I5pjXwwbb12hDytZAiVzUmNzi1Ae2CCvOnL6QCpqzsJT7fWWjXXi&limit=25&offset=25&__after_id=enc_AdB7PJbXYkDSSZAq33AjPXZAeRnlrZBDwjAAILZAg3emHdei0qdRLa2AeD6sRuX6h0OQuPQi8x8bvSHPy0EqIgybYL89""
},
""summary"": {
    ""total_count"": 2
}
}";

      dynamic x = JsonConvert.DeserializeObject(stringVal);
      var data = x.data;
    foreach(var d in data)
    {
      bool installed = d.installed;
      long id = d.id;
      // todo: use the id and installed 
    }
Radin Gospodinov
  • 2,313
  • 13
  • 14
  • Okay I have this set up now but now I get the error that: "Facebook.MiniJSON" does not have a definition for RootObject friends = Json.Deserialize (result.RawResult); So I am guessing I need to write the code like this: RootObject friends = Json.Deserialize (result.RawResult) as RootObject; ? – coder4life22 Jan 04 '16 at 07:21
  • I edited my answer, try with "dynamic" in that case you don't have to create classes. All members matches the case and the name of the fields in the json, so be careful because you will get null for properties that does not match the json field name. – Radin Gospodinov Jan 04 '16 at 07:47
  • @coder4life22 did you try my solution , it works end to end, just copy paste. – loneshark99 Jan 04 '16 at 07:55
0

Run this in Linqpad and you it will return the installed property.

void Main()
{
    string jsonText =
@"{
    ""data"": [
    {
  ""installed"": true,
      ""id"": ""1292282928282""
},
{
    ""installed"": true,
  ""id"": ""29282829292""
}
],
""paging"": {
    ""next"": ""https://graph.facebook.com/v2.5/105157539862931/friends?fields=installed&format=json&access_token=CAALVPHznNpcBAOnO94HvqUgYKI2kObPZBgR0sqIOMSRO9swZBBTWHb6FjliZCT1KyCmPbnX42xvtngboh3DjFOrixw0pSenwRZA1oXZAHNDdYcGsHNOHjQcZB0f6fsZBQJjhOTttwQu7E5hZBDcAWJVZBGK2AxrZBDZBxLL7I5pjXwwbb12hDytZAiVzUmNzi1Ae2CCvOnL6QCpqzsJT7fWWjXXi&limit=25&offset=25&__after_id=enc_AdB7PJbXYkDSSZAq33AjPXZAeRnlrZBDwjAAILZAg3emHdei0qdRLa2AeD6sRuX6h0OQuPQi8x8bvSHPy0EqIgybYL89""
},
""summary"": {
    ""total_count"": 2
}
}";

 var x = JsonConvert.DeserializeObject<Response>(jsonText);
 //To Get installed
 x.data.Select(d => d.installed).Dump();
}

public class Response
{ 
    public Data[] data;
    public Paging paging;
    public Summary summary;
}

public class Data
{
    public bool installed { get; set; }
    public string id { get; set; }
}

public class Paging
{
    public string next { get; set; }
}

public class Summary
{
    public int total_count { get; set;}
}
loneshark99
  • 706
  • 5
  • 16
0

Consults this example for obtain a friend list using mini json:https://developers.facebook.com/docs/unity/reference/current/Json