Disinfection is not working properly

I created a class to deserialize this json

public class Self
{
    public string href { get; set; }
}

public class Team
{
    public string href { get; set; }
}

public class Links
{
    public Self _self { get; set; }
    public Team team { get; set; }
}

public class Player
{
    public int id { get; set; }
    public string name { get; set; }
    public string position { get; set; }
    public int jerseyNumber { get; set; }
    public string dateOfBirth { get; set; }
    public string nationality { get; set; }
    public string contractUntil { get; set; }
    public string marketValue { get; set; }
}

public class RootObject
{
    public Links _links { get; set; }
    public int count { get; set; }
    public List<Player> players { get; set; }
}

public struct Player_Struct
{
    public string id;
    public string name;
    public string position;
    public int jerseyNumber;
    public string dateOfBirth;
    public string nationality;
    public string contractUntil;
    public string marketValue;
}

So, I created a function to create an HttpRequest and a relative object:

 string requestUrl = teams.link_teams;
 string responseText = parser.Request(requestUrl);
 var obj = JsonConvert.DeserializeObject<Players.RootObject>(responseText);

Now the problem is that the compiler returns this exception:

Unhandled exception "Newtonsoft.Json.JsonSerializationException" in Newtonsoft.Json.dll

Ultralogization: you cannot deserialize the current JSON array (for example, [1,2,3]) to type "SF_DebugProject.API.Players + Links" because it requires a JSON object (for example, {"name": "value"}) to deserialize correctly.

, JSON JSON (, { "name": "value" }), , (, ICollection, IList), , JSON. JsonArrayAttribute , JSON.

'_links', 1, 11.

, fix , :

List<Players.RootObject> obj = JsonConvert.DeserializeObject<List<Players.RootObject>>(responseText);

rootobject, foreach. ?

+4
1

, , JSON RootObject, .

:

public class Rootobject
{
    public _Links _links { get; set; }
    public int count { get; set; }
    public Player[] players { get; set; }
}

public class _Links
{
    public _Self _self { get; set; }
    public Team team { get; set; }
}

public class _Self
{
    public string href { get; set; }
}

public class Team
{
    public string href { get; set; }
}

public class Player
{
    public int id { get; set; }
    public string name { get; set; }
    public string position { get; set; }
    public int jerseyNumber { get; set; }
    public string dateOfBirth { get; set; }
    public string nationality { get; set; }
    public string contractUntil { get; set; }
    public string marketValue { get; set; }
}

So what you need to do is something like the following:

var obj = JsonConvert.DeserializeObject<RootObject>(response);
foreach(var player in obj.players) 
{
     // some stuff
}
+1
source

All Articles