Facebook C#SDKv6如何解析多查询FQL的结果

时间:2012-05-24 06:32:11

标签: json facebook-c#-sdk facebook-fql

由于Prabir's博客,我可以使用facebook c#sdk v 6.14成功执行多查询fql,但需要帮助解析结果。我搜索并尝试了许多方法无济于事。我知道这是一个简单的语法问题,但我对c#和JSON相当新。

非常感谢任何帮助! 谢谢, 乍得

我的代码:

var fb = new FacebookClient(this.FacebookAccessToken);
            dynamic resultsMQFQL = fb.Get("fql",
                            new
                            {
                                q = new
                                {
                                    friendsMovies = "SELECT page_id, uid FROM page_fan WHERE type='MOVIE' AND uid IN (SELECT uid2 FROM friend WHERE uid1=me()) ORDER BY page_id",
                                    movieDetails = "SELECT page_id, name, pic, fan_count, categories, genre, starring, release_date FROM page WHERE page_id IN (SELECT page_id FROM #friendsMovies) ORDER BY fan_count DESC",
                                }
                            });

结果:

{"data":[
    {"name":"friendsMovies",
        "fql_result_set":
            [{"page_id":105638652803531,"uid":796419451},
            {"page_id":113271808686307,"uid":796419451}]},

    {"name":"movieDetails",
        "fql_result_set":[
            {"page_id":105638652803531,"name":"Fear and Loathing in Las Vegas"},
            {"page_id":113271808686307,"name":"Fletch"}
        ]
    }
]}

尝试解析:

foreach (dynamic row in resultsMQFQL.data.fql_result_set)

错误:

'Facebook.JsonArray' does not contain a definition for 'fql_result_set' 

1 个答案:

答案 0 :(得分:1)

好的,想通了......忘了我可以在即时窗口调试时快速尝试各种组合......我说我在编码时相当新吗? :)

public void FBMQFQL()
        {
            var fb = new FacebookClient(this.FacebookAccessToken);
            dynamic resultsMQFQL = fb.Get("fql",
                            new
                            {
                                q = new
                                {
                                    friendsMovies = "SELECT page_id, uid FROM page_fan WHERE type='MOVIE' AND uid IN (SELECT uid2 FROM friend WHERE uid1=me()) ORDER BY page_id",
                                    movieDetails = "SELECT page_id, name, pic, fan_count, categories, genre, starring, release_date FROM page WHERE page_id IN (SELECT page_id FROM #friendsMovies) ORDER BY fan_count DESC",
                                }
                            });

            //To access a direct value: resultsMQFQL.data[0].fql_result_set[0].page_id

            var friendsMovies = resultsMQFQL.data[0].fql_result_set;
            var movieDetails = resultsMQFQL.data[1].fql_result_set;

            if (resultsMQFQL == null)
            {
                //return null;
            }
            else
            {
                //Construct the new, formated, merged datatable to store the results the way we want them   
                DataTable dtMyFriendsMovies = new DataTable();
                dtMyFriendsMovies.Columns.Add("MovieID");
                dtMyFriendsMovies.Columns.Add("FriendUserID");

                foreach (dynamic row in friendsMovies)
                {
                    //Add New DataRow to new DataTable
                    DataRow drRow = dtMyFriendsMovies.NewRow();

                    //Get various values from original JSON Friend List returned
                    drRow["MovieID"] = row.page_id;
                    drRow["FriendUserID"] = row.uid;

                    //Add New Row to New Resulting Data Table
                    dtMyFriendsMovies.Rows.Add(drRow);
                }

                //MovieDetails   
                DataTable dtMovies = new DataTable();

                dtMovies.Columns.Add("movieID");
                dtMovies.Columns.Add("name");
                dtMovies.Columns.Add("pic");
                dtMovies.Columns.Add("fan_count");
                dtMovies.Columns.Add("genre");
                dtMovies.Columns.Add("starring");
                dtMovies.Columns.Add("release_date");

                foreach (dynamic row in movieDetails)
                {
                    //Add New DataRow to new DataTable
                    DataRow drRow = dtMovies.NewRow();

                    //Get various values from original JSON Friend List returned
                    drRow["movieID"] = row.page_id;
                    drRow["name"] = row.name;
                    drRow["pic"] = row.pic;
                    drRow["fan_count"] = row.fan_count;
                    drRow["genre"] = row.genre;
                    drRow["starring"] = row.starring;
                    drRow["release_date"] = row.release_date;

                    //Add New Row to New Resulting Data Table
                    dtMovies.Rows.Add(drRow);
                }
                //return dtMyFriendsMovies;
            }
        } //FB FQL