你好仍然是C#的newb(学生),我想知道如何反序列化这种JSON数据(使用JsonConvert和模型类)
JSON示例:
{
"435321729828514": {
"id": "435321729828514",
"name": "Kursaal Oostende"
},
"56302776046": {
"id": "56302776046",
"name": "Caf\u00e9 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(call):
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=EAAH2ZAZAq846IBAM9ZAX0LWpDxlzFaPr8jNOxDct2tZBw7YJAtnYxIlVud67hiXI51ybmhLcz4AhMtiVxZBBcPixx9wB9ntF1ZBRhSIuSxeUu83mg6tZBc0BseLpdmkWuu7bohQxXvvLUe67pjETnqDOj8PzFZAXHHAyqEqYrWOXvAZDZD\u002522&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;
}
}
答案 0 :(得分:3)
这是你必须要做的。
第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; }
}