无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“portal.Models.SubProjectViewRecord”
我有这个简单的代码,它将使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然,从错误看来,数组中有多个项目,这就是 api 响应返回任何数组的原因。您可以为其使用
List
,其代码如下:虽然您的模型如下,假设 json 数组元素是 json 对象其中的
Id
和Name
成员: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 :While your model would be below assuming that the json array elements are of json object with
Id
andName
members in it:首先,您需要知道 JSON 如何返回数据。因此,如果您有类似的东西:
您需要的是实现类似结构的类的列表(如 SubProjectViewRecord)。所以,你会得到类似的结果:
如果你看到了,这正是你的错误所说的。它说:“我无法将列表分配给 SubProjectViewRecord 变量”。
因此,请确保:
First of all, you'll need to know how is your data returned by your JSON. So, if you have something like that:
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:
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: