简体   繁体   English

JsonSerializationException:无法将当前 JSON object(例如 {"name":"value"})反序列化为类型 'System.Collections。

[英]JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List

When I consume the Web API in the MVC project I encountered this error but I cannot fix this error.当我在 MVC 项目中使用 Web API 时遇到此错误,但无法修复此错误。 How can I solve this?我该如何解决这个问题? My API is clearly working but MVC is doesn't work.我的 API 显然可以工作,但 MVC 不起作用。

Controller

public async Task<IActionResult> ListCountries()
    {
        List<Country> countries = new List<Country>();
        HttpClient _client = new HttpClient();
        HttpResponseMessage _response = new HttpResponseMessage();
        _client = _apiHelper.Initial();
        _response = await _client.GetAsync("api/Countries/getall");
        if (_response.IsSuccessStatusCode)
        {
            var results = _response.Content.ReadAsStringAsync().Result;
            countries = JsonConvert.DeserializeObject<List<Country>>(results);
        }   

        return View(countries);
    }

JSON Data

"data": [
{
  "id": 1,
  "countryName": "Afghanistan"
},
{
  "id": 2,
  "countryName": "Albania"
},
{
  "id": 3,
  "countryName": "Algeria"
},

If you built the Web Api change its response from如果您构建 Web Api 将其响应更改为

"data": [
{
  "id": 1,
  "countryName": "Afghanistan"
},
{
  "id": 2,
  "countryName": "Albania"
},
{
  "id": 3,
  "countryName": "Algeria"
}]

To

 [
        {
          "id": 1,
          "countryName": "Afghanistan"
        },
        {
          "id": 2,
          "countryName": "Albania"
        },
        {
          "id": 3,
          "countryName": "Algeria"
        }
]

Create the object and cast it from json response to object创建 object 并将其从 json 响应转换为 object

public class Data
    {
        public int id { get; set; }
        public string countryName { get; set; }
    }

    public class Root
    {
        public List<Data> data { get; set; }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型&#39;System.Collections.Generic.List`1&#39; - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1' 无法将当前 JSON object(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1 - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1 无法反序列化当前JSON对象(例如{“ name”:“ value”})为类型&#39;System.Collections.Generic.List - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List JSON.Net - 无法将当前json对象(例如{“name”:“value”})反序列化为类型&#39;system.collections.generic.list`1 - JSON.Net - cannot deserialize the current json object (e.g. {“name”:“value”}) into type 'system.collections.generic.list`1 错误:无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型 'System.Collections.Generic.List`1 - Error: Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1 JSON .NET-将当前JSON对象(例如{“ name”:“ value”})反序列化为类型&#39;System.Collections.Generic.List`1 - JSON .NET - annot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型“ System.Collections.Generic.ICollection`1 [System.Double]” - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.ICollection`1[System.Double]' 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型“ System.Collections.Generic.IEnumerable`1 [WebApplication1.UserInfo]” - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.IEnumerable`1[WebApplication1.UserInfo]' 无法将当前 JSON 对象(例如 {&quot;name&quot;:&quot;value&quot;})反序列化为类型 &#39;System.Collections.Generic.IList 问题 - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList problem Newtonsoft.Json.JsonSerializationException: '无法反序列化当前的 JSON object (例如 {"name":"value"}) - Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"})
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM