如何处理这个 JSON 数据?

发布于 2024-11-01 04:42:19 字数 1856 浏览 1 评论 0原文

我刚刚发现 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2024-11-08 04:42:19

扭曲用户记录的对象是字典。

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>();

如果您确实需要将数据作为列表,您可以执行类似的操作。

List<FbUser> userlist = JsonConvert.DeserializeObject<Dictionary<string,FbUser>().Values.ToList<FbUser>();

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>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文