gpt4 book ai didi

c# - 尝试从 URL 反序列化 .NET 的 JSON,但出现错误

转载 作者:太空宇宙 更新时间:2023-11-03 21:35:22 26 4
gpt4 key购买 nike

我正在尝试使用以下代码反序列化 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.

网址是:http://www.classicube.net/api/serverlist/

最佳答案

这是一个json数组。使用

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

关于c# - 尝试从 URL 反序列化 .NET 的 JSON,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22114429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com