I’m currently working with the Facebook API in c#, using the NewtonSoft JSON library to consume all the returned API data.
Returning a list of user pages I find myself creating one off classes for attributes within the returned JSON, in order to serialize it.
Right now I have this:
public class FacebookPage
{
public string id { get; set; }
public string name { get; set; }
public string link { get; set; }
public string category { get; set; }
public bool is_published { get; set; }
public bool can_post { get; set; }
public int likes { get; set; }
public FacebookPageLocation location { get; set; }
public string phone { get; set; }
public int checkins { get; set; }
public string picture { get; set; }
public FacebookPageCover cover { get; set; }
public string website { get; set; }
public int talking_about_count { get; set; }
public string access_token { get; set; }
}
public class FacebookPageLocation
{
public decimal latitude { get; set; }
public decimal longitude { get; set; }
}
public class FacebookPageCover
{
public string cover_id { get; set; }
public string source { get; set; }
public int offset_y { get; set; }
}
It seems like there must be a better way to do this. I can replace FacebookPageLocation with Dictionary, but how would I go about replacing FacebookCoverPage?
Ideally, I’d love to be able to declare it in a nice nested format, like this
public class FacebookPage
{
id = string,
name = string.
link = string,
category = string,
is_published = bool,
can_post = bool,
likes = int,
location =
{
latitude = decimal,
longtitude = decimal
},
phone = string,
checkins = int,
picture = string,
cover =
{
cover_id = string,
source = string,
offset_y = int
}
website = string,
talking_about_count = int,
access_token = string
}
I realise this won’t work in practice, but is there anything that just makes declaring these kinds of classes neater? Or unnecessary?
Instead of declaring a lot classes, i would use
dynamic. For the sample json below–
code would be