我是c#的新手,想将以下数据添加到listview中 http://live.glidernet.org/flightlog/index.php?a=EHDL&s=QFE&u=M&z=2&p=&d=30052015&j 我想在foreach航班上创建一个listview项目,我设法通过以下代码添加一个子项目。
ListViewItem lvi = new ListViewItem("Foo bar");
lvi.SubItems.Add("Foo bar");
lvi.SubItems.Add("Foo bar");
FlarmListView.Items.Add(lvi);
如何将JSON数据解析为此列表视图?
答案 0 :(得分:2)
如果您使用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);