Question

I grab json from facebook like so:

me?fields=albums.fields(id,name,photos.fields(source)), feed.fields(id, message, picture, link, source, object_id, name, description, type).limit(20)

Here is my Json (edited down, but structure intact)

{
  "albums": {
    "data": [
      {
        "id": "812",
        "name": "Timeline Photos",
        "created_time": "2010-07-16T17:14:34-05:00",
        "photos": {
          "data": [
            {
              "source": "https://scontent-b.xx.fbcdn.net/hphotos-prn2/t1.0-9/482_800000000000992_775_n.jpg",
              "id": "800000000000992",
              "created_time": "2014-04-24T13:05:39-05:00"
            }           
          ]
        }
      }
    ]
   },
  "feed": {
    "data": [
      {
        "id": "827_805407852819991",
        "message": "Picture Description!!",
        "picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-prn2/t1.0-0/482_800000000000992_775_s.jpg",
        "link": "https://www.facebook.com/photo.php?fbid=800000000000992&set=a.138763042817812.20386.117612894932827&type=1&relevant_count=1",
        "object_id": "800000000000992",
        "type": "photo",
        "created_time": "2014-04-24T13:05:39-05:00"
      }
    ]
  },
  "id": "827"
}

The reason I grab the albums table is because for some reason "feed" is not containing a "source". Is there a way to use linq to put the source in the feed field for the picture? You can see in albums.data.photos.id = feed.data.object_id I think an inner join in linq might would work, but am unsure how to do it. Thanks for your help.

Was it helpful?

Solution

This will join the data in the way you want, at least for your example:

var root = JsonConvert.DeserializeObject<RootObject>(s);
var q = from albumData in root.albums.data
        from photoData in albumData.photos.data
        join feedData in root.feed.data on photoData.id equals feedData.object_id
        select new { feedData.message, photoData.source }; // or whatever

Using the following classes, generated by http://json2csharp.com/:

public class Datum2
{
    public string source { get; set; }
    public string id { get; set; }
    public string created_time { get; set; }
}

public class Photos
{
    public List<Datum2> data { get; set; }
}

public class Datum
{
    public string id { get; set; }
    public string name { get; set; }
    public string created_time { get; set; }
    public Photos photos { get; set; }
}

public class Albums
{
    public List<Datum> data { get; set; }
}

public class Datum3
{
    public string id { get; set; }
    public string message { get; set; }
    public string picture { get; set; }
    public string link { get; set; }
    public string object_id { get; set; }
    public string type { get; set; }
    public string created_time { get; set; }
}

public class Feed
{
    public List<Datum3> data { get; set; }
}

public class RootObject
{
    public Albums albums { get; set; }
    public Feed feed { get; set; }
    public string id { get; set; }
}

See the LINQ documentation for more on from, join, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top