C#从随机密钥开始排序使用Facebook JSON [英] C# Deserialize Facebook JSON starting with random key
问题描述
JSON示例:
{
435321729828514:{
id:435321729828514,
name:Kursaal Oostende
},
56302776046:{
id:56302776046,
name:Caf\\\é Charlatan
}
}
资料库类:
public class FB
{
public async static任务< FBModel> Entries(string id)
{
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);
返回条目;
}
else
返回null;
}
}
}
型号:
public class FBModel
{
public string ID {get;组; }
public string Name {get;组; }
public override string ToString()
{
返回ID +:+名称;
}
}
MainPage.xaml(call):
private static FBModel _entries; //全局变量
// ...
_entries = await FB.Entries(ids_to_pass);
---------已解决(型号)----- -----
public class FBModel
{
#region properties
public string Id {get;组; }
public string Name {get;组; }
public Events事件{get;组; }
#endregion
}
public class Events
{
#region props
public List< Datum>数据{get;组; }
public Paging Paging {get;组;
#endregion
}
public class Datum
{
#region props
public string Description {get;组; }
public string End_time {get;组; }
public string Name {get;组; }
public Place Place {get;组; }
public string Start_time {get;组; }
public string Id {get;组;
#endregion
}
public class Place
{
#region props
public string Id {get;组; }
public string Name {get;组; }
public Location Location {get;组; }
#endregion
}
public class Location
{
#region props
public string City {get;组; }
public string国家{get;组; }
public double Latitude {get;组; }
public double Longitude {get;组; }
public string Street {get;组; }
public string Zip {get;组;
#endregion
}
#region不重要
public class Paging
{
#region props
public Cursors游标{get;组; }
public string Next {get;组;
#endregion
}
public class Cursors
{
#region props
public string Before {get;组; }
public string {get;组; }
#endregion
}
------ - 已解决(完整的JSON)----------
{
435321729828514:{
id:435321729828514,
name:Kursaal Oostende,
events:{
data:[
{
description:CHRISTOFF,...,
end_time:2017-11-25T23:00:00 + 0100,
name:
place:{
name:Kursaal Oostende,
location:{
city:Oostende,
country:比利时,
纬度:51.2312299,
longitude:2.9126599,
st reet:Westhelling 12,
zip:8400
},
id:435321729828514
},
start_time 2017-11-25T20:00:00 + 0100,
id:161310354323914
}
],
分页:{
$$$
,
next:https://graph.facebook:$$$$$$$ COM / V2.8 / 435321729828514 /事件=的access_token&EAAH2ZAZAq846IBAM9ZAX0LWpDxlzFaPr8jNOxDct2tZBw7YJAtnYxIlVud67hiXI51ybmhLcz4AhMtiVxZBBcPixx9wB9ntF1ZBRhSIuSxeUu83mg6tZBc0BseLpdmkWuu7bohQxXvvLUe67pjETnqDOj8PzFZAXHHAyqEqYrWOXvAZDZD\\\%22放大器;漂亮= 1&安培;限额= 1&放大器;后= MTYxMzEwMzU0MzIzOTE0\"
}
}
}
--------已解决(存储库) ---------
public async static任务<列表&FBModel>> Entries(string id)
{
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);
列表< FBModel> data = entries.Select(item => item.Value).ToList();
返回数据;
}
else
返回null;
}
}
是你要做的。
步骤1:将json转换为字典。
var dataDictionary = JsonConvert.DeserializeObject< Dictionary< string,FBModel>>(yourJsonstring);
步骤2:然后获取对象列表
列表< FBModel> data = new List< FBModel>();
foreach(dataDictionary中的var item)
{
data.Add(item.Value);
}
步骤2可以作为一个linq查询完成
列表< FBModel> data = dataDictionary.Select(item => item.Value).ToList();
更新
您的类结构应如下所示事件数据。
public class FBModel
{
public string ID {get;组; }
public string Name {get;组; }
public Events事件{get;组; }
public override string ToString()
{
返回ID +:+名称;
}
}
public class Events
{
public List< Data>数据{get;组; }
}
public class Data
{
public string Description {get;组; }
public string End_Time {get;组; }
public string Name {get;组; }
public Place Place {get;组; }
public string Start_Time {get;组; }
public string Id {get;组;
}
public class Place
{
public string Name {get;组; }
public Location Location {get;组;
}
public class Location
{
public string City {get;组; }
public string国家{get;组; }
public string Latitude {get;组; }
public string Longitude {get;组; }
public string Street {get;组; }
public string Zip {get;组;
}
Hello still a newb (student) to C#, I was wondering how to deserialize this kind of JSON data (with JsonConvert and a model class)
JSON example:
{
"435321729828514": {
"id": "435321729828514",
"name": "Kursaal Oostende"
},
"56302776046": {
"id": "56302776046",
"name": "Caf\u00e9 Charlatan"
}
}
Repository class:
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;
}
}
}
Model:
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);
--------- Solved (model) ----------
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
}
-------- Solved (complete 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"
}
}
}
}
-------- Solved (Repository) ---------
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;
}
}
This is what you have to do.
Step 1: Convert your json to Dictionary.
var dataDictionary = JsonConvert.DeserializeObject<Dictionary<string, FBModel>>(yourJsonstring);
Step 2: Then get list of object
List<FBModel> data=new List<FBModel>();
foreach (var item in dataDictionary)
{
data.Add(item.Value);
}
Step 2 can be done as a linq query
List<FBModel> data= dataDictionary.Select(item => item.Value).ToList();
Update your class structure should be like below to access the event data.
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; }
}
这篇关于C#从随机密钥开始排序使用Facebook JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!