将JSON数据追加到ListView C#

问题描述:

我是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);