JSon.net linq join

本文关键字:join linq net JSon | 更新日期: 2023-09-27 18:00:40

我从facebook上抓取json,就像这样:

我?fields=相册.fields(id,name,photos.fields(source)),提要.fields)(id,message,picture,link,source,object_id,name,description,type).limit(20)

这是我的Json(编辑下来,但结构完整)

{
  "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"
}

我之所以抓取相册表,是因为出于某种原因,"提要"不包含"源"。有没有一种方法可以使用linq将源放在图片的提要字段中?您可以在albums.data.photos.id=feed.data.object_id中看到我认为内部加入linq可能会奏效,但不确定如何做到。谢谢你的帮助。

JSon.net linq join

这将以您想要的方式连接数据,至少对于您的示例:

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

使用以下类,由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; }
}

有关fromjoin等的更多信息,请参阅LINQ文档。