我嘗試使用下面的代碼反序列化網址:試圖Deserialise JSON用於.NET從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; }
}
但我得到一個錯誤:
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.
的URL是:http://www.classicube.net/api/serverlist/
錯誤消息告訴您問題是 - 什麼只是說:) – theMayer