Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot deserialize the current JSON array (e.g. [1,2,3]). C#, cant figure the error out

I'm trying to retrieve all names from the following json data and put it in a textbox.

This is the json data I have shortened some values to make it easier to read but it does not effect the question.

    [{
"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 ]}]

However I cant seam to fetch all these names without getting this error:

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.

This is my class:

        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; }  

    }

This is the method to retrieve information from all sets.

        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;
    }

And this is the code I use in my form.

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

Can someone help me? Thank you for reading

like image 262
RedZerg Avatar asked Mar 14 '15 12:03

RedZerg


People also ask

Can not deserialize current JSON array?

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.

What does Jsonconvert DeserializeObject do?

DeserializeObject Method. Deserializes the JSON to a . NET object.

How do you represent an array of objects in JSON?

A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.

How do I fix JSON object deserialization error?

To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object.

Why is JSON crying about my deserialization?

Your JSON entity is an array rather than a single object. This is why JSON.NET is crying about your deserialization! Thanks for contributing an answer to Stack Overflow!

How to deserialize a dashboardviewmodel from a JSON array?

JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path 'DashboardViewModel', line 1, position 16.


1 Answers

Your JSON entity is an array rather than a single object. This is why JSON.NET is crying about your deserialization!

Deserialize this JSON as an IEnumerable<T> and you'll solve your issue:

IEnumerable<CardSet> result = JsonConvert.DeserializeObject<IEnumerable<CardSet>>(jsonText);
like image 186
Matías Fidemraizer Avatar answered Oct 19 '22 18:10

Matías Fidemraizer