gpt4 book ai didi

c# - 使用 C# (WinForms) 在 DataGridView 中显示 FQL 结果

转载 作者:行者123 更新时间:2023-11-30 20:52:31 25 4
gpt4 key购买 nike

我有 FQL 查询和访问 token 。但是如何使用 C# 和 ASP.NET 在 GRID VIEW 中显示 FQL 结果?

FQL:

SELECT uid, username, first_name, last_name, friend_count, pic_big  
FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

这是我尝试过的:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Facebook;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
var fb = new FacebookClient("{My Access Token}");

dynamic result = fb.Get("/me");
var name = result.name;

MessageBox.Show("Hi " + name);
}
}
}

我已经意识到数据是以 JSON 形式从 Facebook 返回的,所以我尝试反序列化,但现在还是很幸运。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Facebook;
using System.Dynamic;
using Newtonsoft.Json;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}


public class MyFriends
{
public int uid {get; set;}
public string username {get; set;}
public string first_name {get; set;}
public string last_name {get; set;}
public int friend_count {get; set;}
public string pic_big {get; set;}
}


private void button1_Click(object sender, EventArgs e)
{
var fb = new FacebookClient("2A64ZAIeJVIbdZAxXRZCwYf5Bg27OgZDZD");

var query = string.Format("SELECT uid, username, first_name, last_name, friend_count, pic_big FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())");

dynamic parameters = new ExpandoObject();
parameters.q = query;
dynamic results = fb.Get("/fql", parameters);

results = JsonConvert.DeserializeObject<MyFriends>(results);

//MessageBox.Show("Hi " + results);
}
}
}

最佳答案

C# Winforms 解决方案使用标题中指定的 DataGridView 控件。


1)修改你的MyFriend,我稍微改了一下

public class MyFriends
{
public long uid { get; set; }
public string username { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public long? friend_count { get; set; } //nullable
public string pic_big { get; set; }
}

2) 将 DataGridView 控件添加到您的表单

3) 使用 JsonConvert 将您的 result.data 反序列化到列表。您还应该传递 result.data.ToString() 否则它会给出异常。

4) 绑定(bind)您的新列表到 DataGridView.Datasource

var fb = new FacebookClient("accessToken");

var query = string.Format(@"SELECT uid, username, first_name, last_name, friend_count, pic_big
FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())");

dynamic parameters = new ExpandoObject();
parameters.q = query;
dynamic results = fb.Get("/fql", parameters);

List<MyFriends> q = JsonConvert.DeserializeObject<List<MyFriends>>(results.data.ToString());

dataGridView1.DataSource = q;

结果: enter image description here

关于c# - 使用 C# (WinForms) 在 DataGridView 中显示 FQL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20825015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com