从webservice处理数组
本文关键字:数组 处理 webservice | 更新日期: 2023-09-27 18:04:18
我有一个返回数组/锯齿数组的数组的web服务。
我在本地c# windows窗体应用程序中处理它有问题。
最初它给我内容类型错误。现在使用示例代码,它返回给我一个空数组。
我也尝试返回一个一维数组,但结果是相同的。
WebService:
[WebMethod]
public string[] teste()
{
string[] a = new string[1] { "one" };
string[] b = new string[1] { "two" };
string[][] c = { a, b };
return c;
}
局部面:
class open_notes
{
public static ServiceReference1.Smart_Stick_ServiceSoapClient web_service = new ServiceReference1.Smart_Stick_ServiceSoapClient();
public static void open()
{
string[][] a = null;
a [0][0] = web_service.teste().ToString();
MessageBox.Show(a[0][0]);
}
}
您的web服务没有返回类型string[][]
,您不应该在web服务调用的结果上调用.ToString()
,并且当您设置string[][] a = null
然后尝试索引a[0][0]
时,您正在访问空引用。只需将变量设置为web服务调用的结果
public static void open()
{
var a = web_service.teste();
}
你的函数正在返回一个字符串数组(相对于字符串数组的数组),你正在对结果数组调用ToString()。
谢谢大家的帮助
但是最后我认为最好在Web Service中创建一个结构体。
但是你不能忘记,而不是在客户端再次创建结构,你需要实例化它,使web服务的引用。
服务器端:public struct ClientData
{
public string descricao;
public string timer;
}
[WebMethod]
public ClientData[] teste()
{
ClientData[] Clients = null;
Clients = new ClientData[30];
Clients[0].descricao = "oi";
Clients[0].timer = "legal";
return Clients;
}
客户端:
public static void receive_teste()
{
WindowsFormsApplication1.ServiceReference1.ClientData[] Clients = null;
Clients = new WindowsFormsApplication1.ServiceReference1.ClientData[30];
Clients = (WindowsFormsApplication1.ServiceReference1.ClientData[])web_service.teste();
MessageBox.Show(Clients[0].descricao.ToString()); // Shows returned "descricao"
MessageBox.Show(Clients[0].timer.ToString()); // Shows returned "timer"
}