如何处理这个 JSON 数据?
我刚刚发现 facebook 使用 FQL 方法返回的 JSON 数据的格式与 facebook graph api 返回的 JSON 的格式不同。
例如:
FQL <> GRAPH
uid <> id
work_history <> work
education_history <> education
字段名称以及数组中对象的结构也不同。
我在我的网站上使用了 FQL 和 GRAPH。因为我不想创建 2 个 facebook 用户类。我决定只使用图形 api。
我现在正在尝试获取所有用户朋友的列表,包括工作和教育数据。
我不确定您是否需要我的代码,但如果您需要它来解决我的问题,这里是: (我想要处理的json字符串在下面)
public static List<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;
}
}
之后,在json变量中,我得到以下数据:
[{"505757632":{"id":"505757632","name":"Cecilia demo","first_name":"Cecilia", ....},
{"507493765":{"id":"507493765","name":"Bjorn demo","first_name":"Bjorn", .....}
, ... ]
我不知道如何从这些数据中获取用户对象的列表。 用户对象具有以下字段:id,name,first_name,...所以我需要摆脱包裹在我的用户对象周围的附加对象。
非常感谢任何帮助!
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 List<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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
扭曲用户记录的对象是字典。
如果您确实需要将数据作为列表,您可以执行类似的操作。
The object that is warping the user records is a Dictionary.
If you actually need the data as a list, you can do something like.