How to handle this JSON data?
I just found out the JSON data returned by facebook using the FQL method has a different format than the JSON returned by the facebook graph api.
For example:
FQL <> GRAPH
uid <> id
work_history <> work
education_history <> education
Also the field names, and the structure of objects in arrays is different.
I was using FQL and GRAPH for my website. Because i don't want to make 2 facebook user classes. I've decided to use only the graph api.
I'm trying now to get a list of al user's friends including there work and education data.
I'm not sure if you need my code, but if you need it to solve my problem, here it is: (the json string i want to handle is more below)
public static L开发者_JAVA技巧ist<FbUser> getFriends(string id, string token)
{
//get all id's of friends
string jsonUrl = graphUrl + "/" + id + "/friends?access_token=" + token;
var json = new WebClient().DownloadString(jsonUrl);
FbUserList useridlist = JsonConvert.DeserializeObject<FbUserList>(json);
//get all data of friends
jsonUrl = graphUrl + "?ids=" + useridlist.getIds() + "&access_token=" + token;
json = new WebClient().DownloadString(jsonUrl);
List<FbUser> userlist = JsonConvert.DeserializeObject<List<FbUser>>(json.Insert(0,"[") + "]");
return userlist;
}
useridlist.cs
public class FbUserList
{
private List<FbObject> _data;
public List<FbObject> data
{
get
{
return _data;
}
set
{
_data = value;
}
}
public string getIds()
{
string result = "";
foreach (FbObject o in _data) {
result += o.id + ",";
}
string test = result.Substring(0, result.Length - 1);
return test;
}
}
After this, in the json variable, i get the following data:
[{"505757632":{"id":"505757632","name":"Cecilia demo","first_name":"Cecilia", ....},
{"507493765":{"id":"507493765","name":"Bjorn demo","first_name":"Bjorn", .....}
, ... ]
I don't know how i can get a list of user objects from this data. a user object has fields like: id, name, first_name, ... So i need to get rid of the additional object that is wrapped around my user object.
Any help is very much appreciated!
The object that is warping the user records is a Dictionary.
class FbUser()
{
public string id { get;set;}
public string name { get;set;}
public string first_name { get;set;}
.... a property name for every data element in the returned record.
}
Dictionary<string,FbUser> userlist = JsonConvert.DeserializeObject<Dictionary<string,FbUser>();
If you actually need the data as a list, you can do something like.
List<FbUser> userlist = JsonConvert.DeserializeObject<Dictionary<string,FbUser>().Values.ToList<FbUser>();
精彩评论