您好我正在接受以下回复的json。现在我想提取每个数据并需要插入到sqlite表中。如何遍历每个json数据?
private async System.Threading.Tasks.Task SyncAsync()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:56666/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("Api/student");
if (response.IsSuccessStatusCode)
{
var stdetails=response.Content.ReadAsStringAsync();
}
}
}
导致json:
[{"ID":1,"name":"Shyam","class":"a"},{"ID":2,"name":"Bran","class":"b"}]
答案 0 :(得分:2)
创建一个代表您的JSON对象的类 - 在您的情况下 -
public class MyModel
{
public int ID { get; set; }
public string name { get; set; }
public string @class { get; set; }
}
然后使用下面的行将数据反序列化为必需的类
var jsonData = JsonConvert.DeserializeObject<List<MyModel>>(stdetails);