Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

receive JSON array as argument in web service

I'm using a web service programmed in Visual Basic .NET 3.5 to receive a JSON Array sent from other application.

I'm sending a JSON string like this one:

[{"idRecoleccion":1,"PIN":"553648138"},{"idRecoleccion":2,"PIN":"553648138"}]

And I'm receiving the code in Visual Basic .NET as follows:

<WebMethod()> _
Public Function ConfirmaRecol(ByVal ArrayConfirma() As cConfirmaRecoleccion) As Boolean  
     For X = 0 To ArrayConfirma.Length -1
              ' Usage example
              ArrayConfirma(X).PIN
              ArrayConfirma(X).idRecoleccion
         End If
    Next
End Function

And I'm defining the class cConfirmaRecolecciones this way:

Public Class cConfirmaRecoleccion
Dim p_idRecoleccion As Integer
Dim p_PIN As String

Public Property idRecoleccion() As Integer
  Get
      Return p_idRecoleccion
  End Get
  Set(ByVal value As Integer)
      p_idRecoleccion = value
  End Set
End Property

Public Property PIN() As String
  Get
      Return p_PIN
  End Get
  Set(ByVal value As String)
      p_PIN = value
  End Set
End Property
End Class

I just want to be able to use the array of 1 integer and 1 string that was sent by JSON that's why I'm trying to use it his way.

Few important things to know:

  • I've been able to use web services with methods that have simpler arguments so the configurations that I have to use web services are correct.

This is the error I'm getting by the server:

{"Message":"Type \u0027System.Collections.Generic.IDictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]\u0027 is not supported for deserialization of an array.","StackTrace":"   at System.Web.Script.Serialization.ObjectConverter.ConvertListToObject(IList list, Type type, JavaScriptSerializer serializer, Boolean throwOnError, IList& convertedList)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)\r\n   at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n   at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n   at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

Do you gurus have any other suggestion on how I can receive an Array of custom objects in Visual Basic .NET?

like image 260
Arturo Avatar asked Jul 06 '10 00:07

Arturo


1 Answers

I found the solution, this was the right way:

{"ArrayConfirma": [{"idRecoleccion":1,"PIN":"553648138"},{"idRecoleccion":2,"PIN":"553648138"}]}

I had to specify an array with the name of the argument that the web service method is receiving.

like image 183
Arturo Avatar answered Nov 10 '22 10:11

Arturo