Yunus Kaya February 2016

C# ASP.NET JSON to a JSON array Cannot deserialize the current JSON object

I'm using Json.Net to DeserializeObject Json data into object/collection (list)

I'm not sure what I'm doing wrong and I have tried this:

        public List<RootObject> GetRecipes()
    {
        string summonerID = Session["summonerID"].ToString();
        string downloadedString;
        WebClient client = new WebClient();
        downloadedString = client.DownloadString("https://tr.api.pvp.net/api/lol/tr/v1.3/game/by-summoner/" + summonerID + "/recent?api_key=55686aef-da52-4184-b987-98799662d92e"); //tırnak içerisine istediğiniz web adresini yazınız
        List<RootObject> liste = JsonConvert.DeserializeObject<List<RootObject>>(@downloadedString);
        return liste;
        lblYazi.Text = liste.ToString();
    }

I'm getting this error:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[KYLOL2.WebForm2+RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

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) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'summonerId', line 1, position 14.

My Json link

My Class

public class FellowPlayer
    {
        public int summonerId { get; set; }
        public int teamId { get; set; }
        public int championId { get; set; }
    }

    public class Stats
    {
        public int level { get; set; }
        public int goldEarned { get; set; }
        public int numDeaths { get;        

Answers


Brian C February 2016

You are telling the deserializer to look for RootObject as a List, but it's not in the example you are providing. Something like this should work better:

RootObject liste = JsonConvert.DeserializeObject<RootObject>(@downloadedString);

So something like this:

public RootObject GetRecipes()
{
    string summonerID = Session["summonerID"].ToString();
    string downloadedString;
    WebClient client = new WebClient();
    downloadedString = client.DownloadString("https://tr.api.pvp.net/api/lol/tr/v1.3/game/by-summoner/" + summonerID + "/recent?api_key=55686aef-da52-4184-b987-98799662d92e"); //tırnak içerisine istediğiniz web adresini yazınız
    RootObject liste = JsonConvert.DeserializeObject<RootObject>(@downloadedString);
    return liste;
    lblYazi.Text = liste.ToString();  // never reaches this code
}

or if you need to keep the prototype of the GetRecipes method:

public List<RootObject> GetRecipes()
{
    string summonerID = Session["summonerID"].ToString();
    string downloadedString;
    WebClient client = new WebClient();
    downloadedString = client.DownloadString("https://tr.api.pvp.net/api/lol/tr/v1.3/game/by-summoner/" + summonerID + "/recent?api_key=55686aef-da52-4184-b987-98799662d92e"); //tırnak içerisine istediğiniz web adresini yazınız
    List<RootObject> liste = new List<RootObject>();  // create the return list
    RootObject e = JsonConvert.DeserializeObject<RootObject>(@downloadedString);
    liste.Add(e);  // add the reserialized object to the list we are returning
    return liste;
    lblYazi.Text = liste.ToString();  // never reaches this code
}

Post Status

Asked in February 2016
Viewed 3,080 times
Voted 7
Answered 1 times

Search




Leave an answer