我正在尝试从此函数返回一个数组。这是功能:
public static string[] loadServers() {
WebClient client = new WebClient();
string getString = client.DownloadString("http://flippr.pw/en/web_service/servers.json");
client.Dispose();
Dictionary<string, Servers> listOfServers = JsonConvert.DeserializeObject<Dictionary<string, Servers>>(getString);
return listOfServers;
}
如何将数组返回到声明的数组?
答案 0 :(得分:1)
假设您要返回一个字符串数组,那么以下示例显示了一个方法:
Dictionary<string, int> listOfServers = new Dictionary<string, int>();
listOfServers.Add("Server 1", 123980123);
listOfServers.Add("Server 2", 123234235);
string[] results = listOfServers.Select (x => x.Key).ToArray();
其中listIfServers的int部分代表Servers
类型。对于上面的例子,它只是
return listOfServers.Select (x => x.Key).ToArray();
否则,请将方法的签名更改为Dictionary<string, Servers>
返回类型。
答案 1 :(得分:-4)
你的返回类型是Dictionary,在你的函数中你指定了返回类型为string []。
public static Dictionary<string, Servers> loadServers() {
WebClient client = new WebClient();
string getString = client.DownloadString("http://flippr.pw/en/web_service/servers.json");
client.Dispose();
Dictionary<string, Servers> listOfServers = JsonConvert.DeserializeObject<Dictionary<string, Servers>>(getString);
return listOfServers;
}