Reputation: 4883
I have a JSON array of Timezones that I want to display in a list box.
JSON Structure:
[
{
"value": "Dateline Standard Time",
"abbr": "DST",
"offset": -12,
"isdst": false,
"text": "(UTC-12:00) International Date Line West"
},
{
"value": "UTC-11",
"abbr": "U",
"offset": -11,
"isdst": false,
"text": "(UTC-11:00) Coordinated Universal Time-11"
}
]
On the load of my application, I load the JSON results.
Loading JSON in Application:
public Form1()
{
InitializeComponent();
LoadJson();
}
public void LoadJson()
{
using (StreamReader r = new StreamReader("timeZones.json"))
{
string json = r.ReadToEnd();
List<TimeZones> timeZones = JsonConvert.DeserializeObject<List<TimeZones>>(json);
listBox1.DataSource = timeZones;
}
}
}
public class TimeZones
{
public string Value { get; set; }
public string Abbr { get; set; }
public string Offset { get; set; }
public string IsDst { get; set; }
public string Text { get; set; }
}
When I add this list as the datasource to my listbox, I get the following, cow can I get each line to show Value, Abbr, Offset isDst and Text? What am I missing?
Upvotes: 0
Views: 3117
Reputation: 8111
The easiest way would be to override ToString()
method of TimeZones
like this:
public override string ToString()
{
return string.Format("{0}, {1}, {2}, {3}, {4}",
Value, Abbr, Offset, IsDst, Text);
}
Reference: http://msdn.microsoft.com/library/system.object.tostring%28v=vs.110%29.aspx
Upvotes: 1