How to display a json array in a textbox using c# -
i know how deserialize json array , display on richtextbox. i'm calling api json array. can me out this. have got list i'm not sure whether i've done properly.
form1.cs
private void btnstart_click(object sender, eventargs e) { runapi("http://localhost:8080/json_coordinates"); } public void runapi(string api) { try { webrequest request = webrequest.create(api); httpwebresponse response = (httpwebresponse)request.getresponse(); stream datastream = response.getresponsestream(); streamreader reader = new streamreader(datastream); string json = reader.readtoend(); obj.deserializejsondes(json); // me fill display data on richtextbox //richtextbox1.text = responsefromserver; reader.close(); datastream.close(); response.close(); } catch (exception ex) { } }
class jsondes
class jsondes { public list<jsondes> name { get; set; } public list<jsondes> coordinates { get; set; } public list<jsondes> deserializejsondes(string jsonarray) { //return jsonconvert.deserializeobject<jsondes>(json); return jsonconvert.deserializeobject<list<jsondes>>(jsonarray); } }
the original json being passed in has structure:
[{'name' : 'train 1', 'coordinates' : '38.892802, -77.061945'}, {'name' : 'train 2', 'coordinates' : '38.941686, -77.134043'}]
your class structure not clear.however without knowing try answer question.
what can cast dynamic type jsonconvert.deserializeobject<dynamic>()
deserialize string dynamic type access properties in usual way.
var results = jsonconvert.deserializeobject<dynamic>(jsonarray);
now cane access results[0].name
.
alternatively can return object of type jarray
.
dynobj = (jarray)jsonconvert.deserializeobject(jsonarray);
then iterate on object like
foreach (jobject item in dynobj) { access item["your property name"] }
hope helps you.
Comments
Post a Comment