c# - 无法反序列化当前的 JSON 数组(例如 [1,2,3])。 C#,无法找出错误

标签 c# json api serialization deserialization

我正在尝试从以下 json 数据中检索所有名称并将其放入文本框中。

这是我缩短了一些值以使其更易于阅读的 json 数据,但这不会影响问题。

    [{
"id": "LEA",
"name": "Limited Edition Alpha",
"block": null,
"type": "Core",
"description": "The name Alpha refers to the first print run of the \n  original Magic: The Gathering Limited Edition, the first Magic: The Gathering \n  card set. It premiered in a limited release at Origins Game Fair in 1993, with \n  a general release that August. Its print run of 2.6 million cards sold out very quickly and was replaced by Limited Edition's Beta print run. Limited Edition cards have no expansion symbol, no copyright date, no trademark symbols, although they do list the art credits at the bottom of the card.",
"common": 74,
"uncommon": 95,
"rare": 116,
"mythicRare": 0,
"basicLand": 10,
"total": 295,
"releasedAt": "1993-08-05",
"cardIds": [
  226,
  275,
  245
]  },{
"id": "LEB",
"name": "Limited Edition Beta",
"block": null,
"type": "Core",
"description": "Limited Edition Beta or just Beta refers to the second \n  print run of the original Magic: The Gathering Limited Edition, the first \n  Magic: The Gathering card set. It was released as soon as Wizards of the \n  Coast could afford to pay for the rest of the print run. The company took \n  advantage of the fact that the first edition print run had been split to \n  correct some minor problems in the rules and fix some errors on the cards. \n  Clarifications were made to the rulebook, and Richard Garfield's short fiction \n  'Worzel's Tale' was removed to make room. Like Alpha it had no expansion symbol, \n  and the text on the bottom left consisted of only the artist credit. \n  Although many players speak of Alpha and Beta as different sets, officially \n  they are the same set, and the company had expected that people wouldn't \n  necessarily be able to tell the two press runs apart. However, the printer \n  accidentally used different corner rounding dies for the second run, resulting \n  in the two distinct sets.",
"common": 75,
"uncommon": 95,
"rare": 117,
"mythicRare": 0,
"basicLand": 10,
"total": 297,
"releasedAt": "1993-10-01",
"cardIds": [
  390,
  571,
  361,
  505,
  369,
  315 ]}]

但是我无法在不出现此错误的情况下接缝获取所有这些名称:

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'MTGLibrary.CardSetFind+CardSet' 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.

这是我的课:

        public class CardSet
    {
        public string id { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public string block { get; set; }
        public string description { get; set; }
        public int common { get; set; }
        public int uncommon { get; set; }
        public int rare { get; set; }
        public int mythicRare { get; set; }
        public int basicLand { get; set; }
        public int total { get; set; }
        public DateTime releasedAt { get; set; }
        public int[] cardIds { get; set; }  

    }

这是从所有集合中检索信息的方法。

        public static T _download_serialized_json_data<T>(string url) where T : new()
    {
        using (var w = new WebClient())
        {
            var json_data = string.Empty;
            try
            {
                json_data = w.DownloadString(url);
            }
            catch (Exception) { }
            return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<T>(json_data) : new T();
        }

    }



        public static CardSet allCardSets()
    {
        var url = "http://api.mtgdb.info/sets/";
        var foundSet = _download_serialized_json_data<CardSet>(url);
        CardSet setInfo = foundSet;
        return setInfo;
    }

这是我在表单中使用的代码。

        public void fillBox()
    {
        textBox5.Text = CardSetFind.allCardSets().name;
    }

有人可以帮助我吗? 感谢阅读

最佳答案

您的 JSON 实体是一个数组而不是单个对象。这就是为什么 JSON.NET 为你的反序列化哭泣!

将此 JSON 反序列化为 IEnumerable<T>你会解决你的问题:

IEnumerable<CardSet> result = JsonConvert.DeserializeObject<IEnumerable<CardSet>>(jsonText);

关于c# - 无法反序列化当前的 JSON 数组(例如 [1,2,3])。 C#,无法找出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29049117/

相关文章:

C# 匿名对象作为参数不起作用

Python 将 None 加载到 Json 而不会发出错误

php - "Maximum execution time of 60 seconds is exceeded"当我重写 toArray() 函数时

php - 如何存储来自 JSON api 的 moSTLy 静态数据?

java - Android 应用程序启动后立即关闭

c# - 如何从二进制数组作为资源在 C# 中将程序集加载并执行到内存中

javascript - 在 Angular Controller 中包含 JSON 时出现问题

c# - 是否有用于访问Excel文件的免费API?

c# - Linq 查询返回错误

java - pom.xml 中出现错误。显示连接超时。我的网络代理不允许连接到 Maven