将JSON数据追加到ListView C# [英] Appending json data to listview c#
本文介绍了将JSON数据追加到ListView C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I am new to c# and would like to add the following data to a listview http://live.glidernet.org/flightlog/index.php?a=EHDL&s=QFE&u=M&z=2&p=&d=30052015&j I want to create a listview item foreach flight, I managed to add a subitem by the following code.
ListViewItem lvi = new ListViewItem("Foo bar");
lvi.SubItems.Add("Foo bar");
lvi.SubItems.Add("Foo bar");
FlarmListView.Items.Add(lvi);
如何将JSON数据解析到此列表视图?
How can I parse the JSON data to this listview?
推荐答案
如果使用 Json.Net ,您可以执行以下操作:
If you use Json.Net, you could do something like this:
WebClient client = new WebClient();
string json = client.DownloadString("http://live.glidernet.org/flightlog/index.php?a=EHDL&s=QFE&u=M&z=2&p=&d=30052015&j");
JObject data = JObject.Parse(json);
// create an array of ListViewItems from the JSON
var items = data["flights"]
.Children<JObject>()
.Select(jo => new ListViewItem(new string[]
{
(string)jo["glider"],
(string)jo["takeoff"],
(string)jo["glider_landing"],
(string)jo["glider_time"]
}))
.ToArray();
FlarmListView.View = View.Details;
FlarmListView.FullRowSelect = true;
FlarmListView.Columns.Add("Glider ID", 70);
FlarmListView.Columns.Add("Takeoff Time", 85);
FlarmListView.Columns.Add("Landing Time", 85);
FlarmListView.Columns.Add("Time In Air", 85);
FlarmListView.Items.AddRange(items);
这篇关于将JSON数据追加到ListView C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文