user3006430
user3006430

Reputation: 15

Trying to Deserialise JSON for .NET from a URL, but getting an error

I'm trying to use the following code to deserialize a URL:

     private void RefeshList_Click(object sender, EventArgs e)
        {
            WebClient list = new WebClient();
            string text = list.DownloadString("http://www.classicube.net/api/serverlist/");
            var server = JsonConvert.DeserializeObject<RootObject>(text);
             serverlist.Text = text;

            }
        }
    }

public class RootObject
{
    public string hash { get; set; }
    public string ip { get; set; }
    public int maxplayers { get; set; }
    public string mppass { get; set; }
    public string name { get; set; }
    public int players { get; set; }
    public int port { get; set; }
    public int uptime { get; set; }

}

But I get an 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 '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<T> 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.

The URL is: http://www.classicube.net/api/serverlist/

Upvotes: 1

Views: 1621

Answers (4)

Wasif Hossain
Wasif Hossain

Reputation: 3950

You have to use List<RootObject> to deserialize it properly:

List<RootObject> server = JsonConvert.DeserializeObject<List<RootObject>>(text);

Upvotes: 0

theMayer
theMayer

Reputation: 16167

In addition to the correct answers listed, I want to go a step further. If you examine the JSON response returned from the server, you will see:

[
    {"hash": "84ce710714eedcdfd7ef22d4776671b0", "maxplayers": 64, "name": "All in the Mined", "players": 0, "uptime": 2107198},

The [ at the very beginning denotes an array in JSON syntax. Many times, one might see JSON objects that contain array properties; however, this is not the case here. This means that your code needs to ask for an array back (and most serializers appreciate that arrays are IEnumerables, so they are happy with any IEnumerable).

And if you think about it, that makes a lot of sense. You see about 30 of those objects listed in the JSON response- you need an appropriate data structure to contain all the objects returned. A single object is not a match.

So, you need to change your code very slightly, by adding the List<> into it. That is the only change required here.

var server = JsonConvert.DeserializeObject<List<RootObject>>(text);

Upvotes: 1

user3368207
user3368207

Reputation: 1

"hash": "84ce710714eedcdfd7ef22d4776671b0"
"maxplayers": 64
"name": "All in the Mined"
"players": 0
"uptime": 2106398

You need set your class like that

public class RootObject
{
    public string hash { get; set; }
    public int maxplayers { get; set; }
    public string name { get; set; }
    public int players { get; set; }
    public int uptime { get; set; }

}

Upvotes: -2

L.B
L.B

Reputation: 116108

It is a json array. Use

var server = JsonConvert.DeserializeObject<List<RootObject>>(text);

Upvotes: 5

Related Questions