c#delelialize facebook json从随机密钥开始



你好,还是一个newb(学生)到c#,我想知道如何对这种JSON数据进行认可(使用JSONCONVERT和A Model类)

JSON示例:

            {
               "435321729828514": {
                  "id": "435321729828514",
                  "name": "Kursaal Oostende"
               },
               "56302776046": {
                  "id": "56302776046",
                  "name": "Cafu00e9 Charlatan"
               }
            }

存储库类:

        public class FB
        {
            public async static Task<FBModel> Entries(string ids)
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(@"https://graph.facebook.com/v2.8/");
                    HttpResponseMessage response = await client.GetAsync("?ids="+ ids +"&fields=id,name&access_token=secret_token");
                    if (response.IsSuccessStatusCode)
                    {
                        string s = await response.Content.ReadAsStringAsync();
                        FBModel entries = JsonConvert.DeserializeObject<FBModel>(s);
                        return entries;
                    }
                    else
                        return null;
                }
            }
        }

模型:

        public class FBModel
        {
            public string ID { get; set; }
            public string Name { get; set; }
            public override string ToString()
            {
                return ID + ": " + Name;
            }
        }

mainpage.xaml(呼叫):

        private static FBModel _entries; // global variable
        // ...
        _entries = await FB.Entries(ids_to_pass);

-----------------------------------------

            public class FBModel
            {
                #region properties
                public string Id { get; set; }
                public string Name { get; set; }
                public Events Events { get; set; }
                #endregion
            }
            public class Events
            {
                #region props
                public List<Datum> Data { get; set; }
                public Paging Paging { get; set; }
                #endregion
            }
            public class Datum
            {
                #region props
                public string Description { get; set; }
                public string End_time { get; set; }
                public string Name { get; set; }
                public Place Place { get; set; }
                public string Start_time { get; set; }
                public string Id { get; set; }
                #endregion
            }
            public class Place
            {
                #region props
                public string Id { get; set; }
                public string Name { get; set; }
                public Location Location { get; set; }
                #endregion
            }
            public class Location
            {
                #region props
                public string City { get; set; }
                public string Country { get; set; }
                public double Latitude { get; set; }
                public double Longitude { get; set; }
                public string Street { get; set; }
                public string Zip { get; set; }
                #endregion
            }
            #region not important
            public class Paging
            {
                #region props
                public Cursors Cursors { get; set; }
                public string Next { get; set; }
                #endregion
            }
            public class Cursors
            {
                #region props
                public string Before { get; set; }
                public string After { get; set; }
                #endregion
            }

---------解决(完整的JSON)-----------

        {
           "435321729828514": {
              "id": "435321729828514",
              "name": "Kursaal Oostende",
              "events": {
                 "data": [
                    {
                       "description": "CHRISTOFF, ...",
                       "end_time": "2017-11-25T23:00:00+0100",
                       "name": "Vrienden Voor Het Leven",
                       "place": {
                          "name": "Kursaal Oostende",
                          "location": {
                             "city": "Oostende",
                             "country": "Belgium",
                             "latitude": 51.2312299,
                             "longitude": 2.9126599,
                             "street": "Westhelling 12",
                             "zip": "8400"
                          },
                          "id": "435321729828514"
                       },
                       "start_time": "2017-11-25T20:00:00+0100",
                       "id": "161310354323914"
                    }
                 ],
                 "paging": {
                    "cursors": {
                       "before": "MTYxMzEwMzU0MzIzOTE0",
                       "after": "MTYxMzEwMzU0MzIzOTE0"
                    },
                    "next": "https://graph.facebook.com/v2.8/435321729828514/events?access_token=EAAH2ZAZAq846IBAM9ZAX0LWpDxlzFaPr8jNOxDct2tZBw7YJAtnYxIlVud67hiXI51ybmhLcz4AhMtiVxZBBcPixx9wB9ntF1ZBRhSIuSxeUu83mg6tZBc0BseLpdmkWuu7bohQxXvvLUe67pjETnqDOj8PzFZAXHHAyqEqYrWOXvAZDZDu002522&pretty=1&limit=1&after=MTYxMzEwMzU0MzIzOTE0"
                 }
              }
           }
        }

--------解决(存储库)-------------

            public async static Task<List<FBModel>> Entries(string ids)
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(@"https://graph.facebook.com/v2.8/");
                    HttpResponseMessage response = await client.GetAsync("?ids="+ ids +"&fields=id,name,events.limit(60)&access_token=secret_token");
                    if (response.IsSuccessStatusCode)
                    {
                        string s = await response.Content.ReadAsStringAsync();
                        var entries = JsonConvert.DeserializeObject<Dictionary<string, FBModel>>(s);
                        List<FBModel> data = entries.Select(item => item.Value).ToList();
                        return data;
                    }
                    else
                        return null;
                }
            }

这就是您要做的。

步骤1:将您的JSON转换为字典。

 var dataDictionary = JsonConvert.DeserializeObject<Dictionary<string, FBModel>>(yourJsonstring);

步骤2:然后获取对象列表

        List<FBModel> data=new List<FBModel>();
        foreach (var item in dataDictionary)
        {
           data.Add(item.Value);
        }

步骤2可以作为linq查询

完成
List<FBModel> data= dataDictionary.Select(item => item.Value).ToList();

更新您的班级结构应该在下面访问事件数据。

public class FBModel
{
    public string ID { get; set; }
    public string Name { get; set; }
    public Events Events { get; set; }
    public override string ToString()
    {
        return ID + ": " + Name;
    }
}
public class Events
{
    public List<Data> Data { get; set; }
}

 public class Data
{
    public string Description { get; set; }
    public string End_Time { get; set; }
    public string Name { get; set; }
    public Place Place { get; set; }
    public string Start_Time { get; set; }
    public string Id { get; set; }
}
public class Place
{
    public string Name { get; set; }
    public Location Location { get; set; }
}
public class Location
{
    public string City { get; set; }
    public string Country { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string Street { get; set; }
    public string Zip { get; set; }
}

最新更新