Displaying Specific Fields from Facebook Graph API JSON

三世轮回 提交于 2019-12-11 05:47:55

问题


I'm trying to simply display the list of members in a specific group using the Facebook Graph API. I'm using Newtonsoft.JSON.

Here is the results of my url query:

Graph API Results

I used a JSON class generator and it gave me this:

public class Datum
{
    public string name { get; set; }
    public string id { get; set; }
    public bool administrator { get; set; }
}

public class Cursors
{
    public string before { get; set; }
    public string after { get; set; }
}

public class Paging
{
    public Cursors cursors { get; set; }
}

public class Members
{
    public List<Datum> data { get; set; }
    public Paging paging { get; set; }
}

public class RootObject
{
    public Members members { get; set; }
    public string id { get; set; }
}

I've tried every combination I can think of to display simply the list of members in a multi-line text box, but not sure if this is even the best way to display the list on a Windows Form App.

Could someone help me understand 2 things.

1) What is the best component to display the list of names in a Windows Form App?

2) What is the 1 or 2 lines to generate just the list of names using JsonConvert.DeserializeObject from this?

My raw data is stored in: string responseFromServer = reader.ReadToEnd();


回答1:


To deserialize the JSON into your classes:

RootObject obj = JsonConvert.DeserializeObject<RootObject>(responseFromServer);

To get the member names into a List<string>:

List<string> members = obj.members.data.Select(d => d.name).ToList();

Note: You need to have using System.Linq; at the top of your file in order to use the Select and ToList methods.

As far as displaying the data in a windows form app, there's not a "best" component-- it depends on what you're trying to accomplish as to what control you would choose to use. For example, if all you want to do is display the list of names in a multi-line textbox, you could do this:

textBox1.Text = string.Join("\r\n", members);

If you want to allow the user to be able to select individual names and do something based on that selection, you would probably want to use a ListBox or a ComboBox instead. You can populate a ListBox or ComboBox like this:

listBox1.DisplayMember = "name";
listBox1.DataSource = obj.members.data;

That should be enough to get you started.



来源:https://stackoverflow.com/questions/39292958/displaying-specific-fields-from-facebook-graph-api-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!