我正在尝试从托管在Web服务器上的MySql数据库中获取一些json数据,并填充我想用于在列表中显示我的数据库条目的数据模型。目前,我已经能够对数据进行反序列化,以查找表中有多少条目,但我对如何填充数据模型感到困惑,我可以将其添加到列表中。
我的代码是
public async Task<List<JobItem>> RefreshJobsAsync()
{
JobItems = new List<JobItem>();
var uri = new Uri(string.Format(Constants.RestUrl, Constants.JobsUrl));
try
{
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
var job = JsonConvert.DeserializeObject<RootObject>(content);
Debug.WriteLine("MySql Table has " + job.jobs.records.Count + " jobs entries"); // Output in console is [0:] MySql Table has 3 jobs entries
}
}
catch (Exception ex)
{
Debug.WriteLine(@" ERROR {0}", ex.Message);
}
return JobItems;
}
我使用json2Sharp.com来生成这些类
public class Jobs
{
public List<string> columns { get; set; }
public List<List<object>> records { get; set; }
}
public class RootObject
{
public Jobs jobs { get; set; }
}
我要填充的数据模型是
public class JobItem
{
public string job_id { get; set; }
public string user_id { get; set; }
public string start_date { get; set; }
public string start_time { get; set; }
public string description { get; set; }
public string qualification { get; set; }
public string address { get; set; }
public string minimum_experience { get; set; }
public string position_filled { get; set; }
public DateTime date_posted { get; set; }
}
最后json响应是
{
"jobs": {
"columns": [
"job_id",
"user_id",
"start_date",
"start_time",
"description",
"qualification",
"address",
"minimum_experience",
"position_filled",
"date_posted"
],
"records": [
[
1,
0,
"0000-00-00",
"00:00:00",
"",
"",
"",
"",
"0",
"2017-09-25 17:46:08"
],
[
4,
0,
"0000-00-00",
"00:00:00",
"",
"",
"",
"",
"0",
"2017-09-25 17:46:08"
],
[
5,
0,
"0000-00-00",
"00:00:00",
"",
"",
"",
"",
"0",
"2017-09-25 17:46:08"
]
]
}
}