添加json数据到listview c#
本文关键字:listview 数据 json 添加 | 更新日期: 2023-09-27 18:11:09
我是c#的新手,想将以下数据添加到列表视图http://live.glidernet.org/flightlog/index.php?a=EHDL&s=QFE&u=M&z=2&p=&d=30052015&j我想为每个航班创建一个listview项,我设法通过下面的代码添加一个子项。
ListViewItem lvi = new ListViewItem("Foo bar");
lvi.SubItems.Add("Foo bar");
lvi.SubItems.Add("Foo bar");
FlarmListView.Items.Add(lvi);
我如何解析JSON数据到这个listview?
如果使用Json。Net,您可以这样做:
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);