我正在尝试使用Web服务将数组返回到我的GUI,但我不知道如何从WebResponse
实际拉出数组。
这是GUI中用于调用Web服务的方法:
public static ArrayList getList()
{
String[] list;
WebRequest request = WebRequest.Create("localhost:8080/test");
WebResponse response = request.GetResponse();
list = ??? //<--What do I put here to actually access the returned array?
return response;
}
答案 0 :(得分:4)
对此的答案很大程度上取决于响应的格式。是JSON吗? XML?
如果我们假设它是表示字符串列表的JSON响应,您可以这样做:
using (var response = request.GetResponse() as HttpWebResponse)
{
Stream responseStream = response.GetResponseStream();
using (var reader = new StreamReader(responseStream))
{
// get the response as text
string responseText = reader.ReadToEnd();
// convert from text
List<string> results = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(responseText);
// do something with it
}
}
(这确实需要JSON.net)