gpt4 book ai didi

c# - C#中的JSON反序列化错误

转载 作者:太空狗 更新时间:2023-10-30 00:32:06 24 4
gpt4 key购买 nike

我想将 JSON 对象反序列化为 C#,但出现此异常:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'FYP___Task_1.RootObject' 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 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.

我曾尝试通过在 StackOverflow 上找到的不同解决方案来解决此错误,但没有人奏效。

我使用的 JSON 如下:

[
{
"rating_count": 158271,
"genres": [
"Action",
"Crime",
"Thriller"
],
"rated": "PG-13",
"language": [
"English",
"French",
"Mandarin"
],
"rating": 6.7,
"country": [
"France",
"USA"
],
"release_date": 20021011,
"title": "Transporter\n \"The Transporter\"",
"year": 2002,
"filming_locations": "Avenue de Saissy, Cannes, Alpes-Maritimes, France",
"imdb_id": "tt0293662",
"directors": [
"Corey Yuen"
],
"writers": [
"Luc Besson",
"Robert Mark Kamen"
],
"actors": [
"Jason Statham",
"Qi Shu",
"Matt Schulze",
"François Berléand",
"Ric Young",
"Doug Rand",
"Didier Saint Melin",
"Tonio Descanvelle",
"Laurent Desponds",
"Matthieu Albertini",
"Vincent Nemeth",
"Jean-Yves Bilien",
"Jean-Marie Paris",
"Adrian Dearnell",
"Alfred Lot"
],
"also_known_as": [
"Transporter"
],
"poster": {
"imdb": "http://ia.media-imdb.com/images/M/MV5BMTk2NDc2MDAxN15BMl5BanBnXkFtZTYwNDc1NDY2._V1_SY317_CR3,0,214,317_.jpg",
"cover": "http://imdb-poster.b0.upaiyun.com/000/293/662.jpg!cover?_upt=cd37cf0e1385015165"
},
"runtime": [
"92 min"
],
"type": "M",
"imdb_url": "http://www.imdb.com/title/tt0293662/"
}
]

我正在使用的类:

public class Poster
{
public string imdb { get; set; }
public string cover { get; set; }
}

public class RootObject
{
public int rating_count { get; set; }
public List<string> genres { get; set; }
public string rated { get; set; }
public List<string> language { get; set; }
public double rating { get; set; }
public List<string> country { get; set; }
public int release_date { get; set; }
public string title { get; set; }
public int year { get; set; }
public string filming_locations { get; set; }
public string imdb_id { get; set; }
public List<string> directors { get; set; }
public List<string> writers { get; set; }
public List<string> actors { get; set; }
public List<string> also_known_as { get; set; }
public Poster poster { get; set; }
public List<string> runtime { get; set; }
public string type { get; set; }
public string imdb_url { get; set; }
}

最佳答案

您的 JSON 对象具有 [ {..} ] 结构,这意味着它是一个对象列表。在您的情况下,您的列表只有一个对象,但它仍然是一个列表。您正在尝试做的是将列表变成一个对象,因此您会得到一个异常。

解决方案是将您的 JSON 更改为 {..}(即删除方括号)或将 JSON 反序列化为 RootObject 数组,然后读取第一个,例如:

RootObject[] myArray = json.Deserialize<RootObject[]>("json goes here");
RootObject firstObject = myArray[0];

关于c# - C#中的JSON反序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20104937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com