【发布时间】:2020-07-15 19:12:13
【问题描述】:
我很难反序列化以下 JSON
[{"JobID": 397597,"CustomerId": "ENV"},
{"JobID": 397694,"CustomerId": "UNI015"},
{"JobID": 397836,"CustomerId": "AMA003"}]
JobData Class
public class JobData
{
[JsonProperty("JobID")]
public int JobID;
[JsonProperty("CustomerId")]
public string CustomerId;
}
这是 REST 服务类
public class RESTService
{
readonly HttpClient _client;
public RESTService()
{
_client = new HttpClient();
}
public async Task<List<JobData>> GetJobData(string query)
{
List<JobData> jobData = null;
try
{
var response = await _client.GetAsync(query);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
jobData = JsonConvert.DeserializeObject<List<JobData>>(content);
}
}
catch (Exception ex)
{
Debug.WriteLine("\t\tERROR {0}", ex.Message);
}
return jobData;
}
}
}
我希望将 JSON 响应读入 Xamarin Listview,但未返回任何数据。内容变量返回一个有效的 JSON 字符串。
【问题讨论】:
-
我被
[JsonProperty("SiteID")]搞糊涂了 -
编辑菜单 -> 选择性粘贴 -> 将 JSON 粘贴为类 公共字段与属性不同
-
Could not reproduce the issue。请张贴Minimal, Reproducible Example。并删除
[JsonProperty("SiteID")]。 -
我将 JsonProperty 更改为正确。
标签: c# arrays json xamarin.forms.listview