以下是Postman的JSON
结果
{
"result": [
{
"brands": [],
"services": [],
"addresses": [],
"accessPoints": [],
"Kodu": "100",
"name": "üeticissdss",
"taxNumber": "asd",
"taxOffice": "sada",
"id": "bfb064f4-0732-43ab-9d94-87f6934258fe",
"isActive": true
},
{
"brands": [],
"services": [],
"addresses": [],
"accessPoints": [],
"Kodu": "100",
"name": "üeticissdss",
"taxNumber": "asd",
"taxOffice": "sada",
"id": "bfb064f4-0732-43ab-9d94-87f69d4258fe",
"isActive": true
}
],
"id": 13,
"exception": null,
"status": 5,
"isCanceled": false,
"isCompleted": true,
"isCompletedSuccessfully": true,
"creationOptions": 0,
"asyncState": null,
"isFaulted": false
}
我想尝试反序列化json,如下所示:
[BindProperty]
public List<Manufacturer> manufacturer { get; set; }
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = new Uri(apiParameters.HttpClientBaseAddress);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(apiParameters.DefaultRequestHeadersJson));
customIndexModel.ApiResponseMessage = await httpClient.GetAsync(apiParameters.HttpClientBaseAddress);
if (customIndexModel.ApiResponseMessage.IsSuccessStatusCode)
{
var jsonAsString = customIndexModel.ApiResponseMessage.Content.ReadAsStringAsync().Result;
manufacturer = JsonConvert.DeserializeObject<List<Manufacturer>>(jsonAsString);
}
错误消息:
无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Collections.Generic.List`1 [Q.Entity.Manufacturer.Manufacturer]',因为该类型需要JSON数组(例如[1,2,3])正确反序列化。 要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似的集合类型可以从JSON对象反序列化的数组或List。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。 路径'结果',第1行,第10位。
看起来我想念一些东西,但我无法弄明白。 你能帮帮我吗?
答案 0 :(得分:0)
public class Result
{
public List<object> brands { get; set; }
public List<object> services { get; set; }
public List<object> addresses { get; set; }
public List<object> accessPoints { get; set; }
public string Kodu { get; set; }
public string name { get; set; }
public string taxNumber { get; set; }
public string taxOffice { get; set; }
public string id { get; set; }
public bool isActive { get; set; }
}
public class RootObject
{
public List<Result> result { get; set; }
public int id { get; set; }
public object exception { get; set; }
public int status { get; set; }
public bool isCanceled { get; set; }
public bool isCompleted { get; set; }
public bool isCompletedSuccessfully { get; set; }
public int creationOptions { get; set; }
public object asyncState { get; set; }
public bool isFaulted { get; set; }
}
我将JSON反序列化为dataList
[BindProperty]
public RootObject dataList { get; set; }
var jsonAsString = customIndexModel.ApiResponseMessage.Content.ReadAsStringAsync().Result;
dataList = JsonConvert.DeserializeObject<RootObject>(jsonAsString);
在JSOn反序列化后,我可以在Razor页面中使用结果,如下所示
@foreach (var item in Model.dataList.result)
{
++rowNumber;
<tr>
<td>@item.code</td>
<td>@item.name</td>
<td>@item.isActive</td>
</
谢谢。