无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“portal.Models.SubProjectViewRecord”

发布于 2025-01-13 22:25:17 字数 2313 浏览 1 评论 0原文

我有这个简单的代码,它将使用 url 中的 api json 数组。

public ActionResult ViewRecord()
        {

            ViewBag.Title = "- Geotagging Sub Project Record.";
            var webClient = new WebClient();
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                     | SecurityProtocolType.Tls11
                                     | SecurityProtocolType.Tls12;
            webClient.Headers.Add(HttpRequestHeader.Cookie, "cookievalue");
            string objJson = webClient.DownloadString(@"https://test.com/api/lib_region");

             //here we will map the Json to C# class
            Models.SubProjectViewRecord oop = JsonConvert.DeserializeObject<Models.SubProjectViewRecord>(objJson);                                                                                                               
            return View(oop);
            
    }

我的模型

namespace portal.Models
{
    public class SubProjectViewRecord
    {
        public string Name { get; set; }
        public int Id { get; set; }
    }
}

上面代码的错误是:

'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'portal.Models.SubProjectViewRecord' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.'

那么这些是我在发布某些回复时应用于模型的修复。

public class SubProjectViewRecord
        {
            public List<string> Name { get; set; }
            public List<int> Id { get; set; }
        }

到这一行:

Models.SubProjectViewRecord oop = JsonConvert.DeserializeObject<List<Models.SubProjectViewRecord>>(objJson);

但引发了错误:

Cannot implicitly convert type 'System.Collections.Generic.List<portal.Models.SubProjectViewRecord>' to 'portal.Models.SubProjectViewRecord'

I've got this simple code that will consume api json array from a url.

public ActionResult ViewRecord()
        {

            ViewBag.Title = "- Geotagging Sub Project Record.";
            var webClient = new WebClient();
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                     | SecurityProtocolType.Tls11
                                     | SecurityProtocolType.Tls12;
            webClient.Headers.Add(HttpRequestHeader.Cookie, "cookievalue");
            string objJson = webClient.DownloadString(@"https://test.com/api/lib_region");

             //here we will map the Json to C# class
            Models.SubProjectViewRecord oop = JsonConvert.DeserializeObject<Models.SubProjectViewRecord>(objJson);                                                                                                               
            return View(oop);
            
    }

my model

namespace portal.Models
{
    public class SubProjectViewRecord
    {
        public string Name { get; set; }
        public int Id { get; set; }
    }
}

The error of the code above is:

'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'portal.Models.SubProjectViewRecord' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.'

then these are the fixes I apply to model as some reply being posted.

public class SubProjectViewRecord
        {
            public List<string> Name { get; set; }
            public List<int> Id { get; set; }
        }

to this line:

Models.SubProjectViewRecord oop = JsonConvert.DeserializeObject<List<Models.SubProjectViewRecord>>(objJson);

but the error raised:

Cannot implicitly convert type 'System.Collections.Generic.List<portal.Models.SubProjectViewRecord>' to 'portal.Models.SubProjectViewRecord'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平安喜乐 2025-01-20 22:25:17

显然,从错误看来,数组中有多个项目,这就是 api 响应返回任何数组的原因。您可以为其使用 List ,其代码如下:

List<Models.SubProjectViewRecord> oop = JsonConvert.DeserializeObject<List<Models.SubProjectViewRecord>>(objJson);

虽然您的模型如下,假设 json 数组元素是 json 对象其中的 IdName 成员:

public class SubProjectViewRecord
{
    public string Name { get; set; }
    public int Id { get; set; }
}                           

Apparently looks like from the error that there are multiple items in the array that is why there is any array returned from the api reponse. you can use a List<T> for it and the code for it would be like :

List<Models.SubProjectViewRecord> oop = JsonConvert.DeserializeObject<List<Models.SubProjectViewRecord>>(objJson);

While your model would be below assuming that the json array elements are of json object with Id and Name members in it:

public class SubProjectViewRecord
{
    public string Name { get; set; }
    public int Id { get; set; }
}                           
很酷不放纵 2025-01-20 22:25:17

首先,您需要知道 JSON 如何返回数据。因此,如果您有类似的东西:

[
  {
    "name": "",
    "id": ""
  }
]

您需要的是实现类似结构的类的列表(如 SubProjectViewRecord)。所以,你会得到类似的结果:

List<SubProjectViewRecord> oop = JsonConvert.DeserializeObject<List<SubProjectViewRecord>>(objJson);

如果你看到了,这正是你的错误所说的。它说:“我无法将列表分配给 SubProjectViewRecord 变量”。

因此,请确保:

  • 您的类与 JSON 结构完全匹配。
  • 您为反序列化提供的类(您在 DeserializeObject 上传递的泛型)与将接收反序列化的变量相同。

First of all, you'll need to know how is your data returned by your JSON. So, if you have something like that:

[
  {
    "name": "",
    "id": ""
  }
]

What you'll need is a list of a class that implements a structure like that (like SubProjectViewRecord). So, you'll have something like that:

List<SubProjectViewRecord> oop = JsonConvert.DeserializeObject<List<SubProjectViewRecord>>(objJson);

And if you see, that is exactly what your error says. It says: "I cannot assign a List to a SubProjectViewRecord variable".

So, just be sure of:

  • Your class matches exactly with your JSON structure.
  • The class that you provide for the deserialization (the generic that you pass on DeserializeObject) is the same as the variable that will receive the deserialization.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文