我有这个json值,我想转换为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"
}]
到目前为止,这是我的代码:
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 the request to the server and wait for the response:
using (WebResponse response = await request.GetResponseAsync())
{
// Get a stream representation of the HTTP web response:
using (Stream stream = response.GetResponseStream())
{
// Use this stream to build a JSON document object:
JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
//Code to convert JsonObject to List<T>
// Return the JSON document:
return jsonDoc;
}
}
这是我想要存储值的类:
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; }
}
我怎样才能做到这一点?提前谢谢!
答案 0 :(得分:2)
您需要下载此Nuget Package(右键单击项目&gt;经理Nuget包)
然后你可以使用:
string json = null;
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
json = stream.ReadToEnd();
}
List<cuentaObtener> JsonConvert.DeserializeObject<List<cuentaObtener>>(json);
如果你正在使用HttpClient:
var response = await httpClient.GetAsync(url);
string json = await response.Content.ReadAsStringAsync();
List<cuentaObtener> JsonConvert.DeserializeObject<List<cuentaObtener>>(json);
答案 1 :(得分:0)