c# - Convert JsonObject to List<T> -
i have json value want convert list
[{ "id_cuenta": 1, "nombre": "mercedes luj\u00e1n", "apellido": "llano", "ci": 123, "telefono": 123456789, "dispositivo_id": "355790037549877", "password": "holaa", "created_at": "2016-07-02 11:36:57", "updated_at": "2016-07-09 09:56:53" }]
this code far:
private list<cuentaobtener> mcuenta; btnlogin.click += async(object sender, eventargs e) => { string url = "http://localhost:8000/api/v1/cuenta_ci/" + ci.text + ""; jsonvalue json = await obtenerci(url); private async task<jsonvalue> obtenerci(string url) { httpwebrequest request = (httpwebrequest)httpwebrequest.create(new uri(url)); request.contenttype = "application/json"; request.method = "get"; // send request server , wait response: using (webresponse response = await request.getresponseasync()) { // stream representation of http web response: using (stream stream = response.getresponsestream()) { // use stream build json document object: jsonvalue jsondoc = await task.run(() => jsonobject.load(stream)); //code convert jsonobject list<t> // return json document: return jsondoc; } }
this class want store values:
public class cuentaobtener { public int id_cuenta { get; set; } public string nombre { get; set; } public string apellido { get; set; } public int ci { get; set; } public int telefono { get; set; } public string dispositivo_id { get; set; } public string password { get; set; } }
how can achieve this? thank in advance!
you need download nuget package (right click project > manager nuget packages)
then can use :
string json = null; using (streamreader stream = new streamreader(response.getresponsestream())) { json = stream.readtoend(); } list<cuentaobtener> jsonconvert.deserializeobject<list<cuentaobtener>>(json);
if you're using httpclient:
var response = await httpclient.getasync(url); string json = await response.content.readasstringasync(); list<cuentaobtener> jsonconvert.deserializeobject<list<cuentaobtener>>(json);
Comments
Post a Comment