c# - How do I take the data from a .json file, use only some of the items in it, and populate a combobox with that data? -
what i'm looking take .json file, grab items file, , populate items in combobox filtered data (using newtonsoft / json.net). i'll give example:
(some of the) json file data:
[ { "name": "kerbol", "radius": 261600000, "mass": 1.7565670e+28 }, { "name": "moho", "radius": 250000, "mass": 2.5263617e+21 }, { "name": "eve", "radius": 700000, "mass": 1.2244127e+23 }, ]
this isn't of data, of it. it's info on planets in game "kerbal space program". thing i'm interested in (right now) grabbing every "name" item in .json file. want populate items property in combobox of names (on each line).
i've tried lot of other code filter out, don't understand enough rewrite work how need.
edit: want more .json data later on, i'm trying 1 step @ time.
edit2: windows forms.
here wpf demo, have test on computer, can try.
public partial class mainwindow : window { public mainwindow() { initializecomponent(); var data = mymodel.gettestdata(); var datastr = jsonconvert.serializeobject(data, formatting.indented); using (var writer = new streamwriter(@"d:\data.json", false)) { writer.write(datastr); } using (var reader = new streamreader(@"d:\data.json")) { var recs = jsonconvert.deserializeobject<list<mymodel>>(reader.readtoend()); cbx.itemssource = recs;//cbx combobox cbx.displaymemberpath = "name"; cbx.selectedindex = 0; } } } public class mymodel { public string name { get; set; } public double radius { get; set; } public double mass { get; set; } public static list<mymodel> gettestdata() { return new list<mymodel> { new mymodel {name = "x1", radius = 1, mass = 1}, new mymodel {name = "x2", radius = 2, mass = 2}, new mymodel {name = "x3", radius = 3, mass = 3}, new mymodel {name = "x4", radius = 4, mass = 4}, }; } }
Comments
Post a Comment