0

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

How can I parse the JSON data to this listview?

0

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Perfect Answer But what if we have a JSON structured like this { "data": { "day1": { "mId": "XgzhugCNT", "pageId": "2", "votes": "2,063", "language": "English" }, "day2": { "mId": "9ilrMdX15S", "pageId": "10", "votes": "2,893", "language": "French" }, "day3": { "mId": "9ilert415S", "pageId": "8", "votes": "2,343", "language": "English" } } } @brain-rogers
@SerenityEmmanuel See fiddle here: dotnetfiddle.net/xcIBt7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.