Error al enviar JSON a WebMethod

I am having trouble sending JSON to a WebMethod. Here is the way that I am trying to do it. If there is a better way to do this please let me know. What I am trying to do is save the JSON object off in a database.

JavaScript

function TEST() {
    var str = '[{
        "Key": 6311,
        "Start": "123 Start",
        "End": "456 End ",
        "Date": "2/2/2012",
        "Order": null,
        "EstMiles": 0,
        "Stops": [
            {"StopAddy": "123 Stop Addy "},
            {"StopAddy": "456 Stop Addy"},
            {"StopAddy": "789 Stop Addy"}
        ]
    }]';    // Whitespace added for clarity

    $.ajax({
        type: "POST",
        url: "WebService1.asmx/Test",
        data: str,
        //contentType: "plain/text",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            alert(msg.d);
        },
        error: function(e) {
            alert(e.responseText);
        }
    });
} 

VB.NET

Public Function Test(ByVal o As Object()) As String
    'do your processing  
    Return "success"
End Function

Public Class [Stop]
    Public Property StopAddy() As String
        Get
            Return m_StopAddy
        End Get
        Set(ByVal value As String)
            m_StopAddy = value
        End Set
    End Property
    Private m_StopAddy As String
End Class

Public Class RootObject
    Public Property Key() As Integer
        Get
            Return m_Key
        End Get
        Set(ByVal value As Integer)
            m_Key = value
        End Set
    End Property
    Private m_Key As Integer
    Public Property Start() As String
        Get
            Return m_Start
        End Get
        Set(ByVal value As String)
            m_Start = value
        End Set
    End Property
    Private m_Start As String
    Public Property [End]() As String
        Get
            Return m_End
        End Get
        Set(ByVal value As String)
            m_End = value
        End Set
    End Property
    Private m_End As String
    Public Property [Date]() As String
        Get
            Return m_Date
        End Get
        Set(ByVal value As String)
            m_Date = value
        End Set
    End Property
    Private m_Date As String
    Public Property Order() As Object
        Get
            Return m_Order
        End Get
        Set(ByVal value As Object)
            m_Order = value
        End Set
    End Property
    Private m_Order As Object
    Public Property EstMiles() As Integer
        Get
            Return m_EstMiles
        End Get
        Set(ByVal value As Integer)
            m_EstMiles = value
        End Set
    End Property
    Private m_EstMiles As Integer
    Public Property Stops() As List(Of [Stop])
        Get
            Return m_Stops
        End Get
        Set(ByVal value As List(Of [Stop]))
            m_Stops = value
        End Set
    End Property
    Private m_Stops As List(Of [Stop])
End Class

El error que obtengo es

{"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)
   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"}

preguntado el 01 de febrero de 12 a las 22:02

Why not use a WCF data contract to specify the format of the JSON instead of just accepting Object? -

¿Por qué es tu data: a string? ... why not an object? api.jquery.com/jQuery.ajax the ajax method will convert it to a string itself? -

1 Respuestas

Based on your stack trace, I assume you are using Asp.net on the server side? If so, I believe Asp.net requires you to have a main object that wraps list of objects for json data. So, please change your input to the following, and change your server code a bit to handle the extra wrapping layer:

var str = '{
"data": 
[
    {
        "Key": 6311,
        "Start": "123 Start",
        "End": "456 End ",
        "Date": "2/2/2012",
        "Order": null,
        "EstMiles": 0,
        "Stops": [
            {
                "StopAddy": "123 Stop Addy "
            },
            {
                "StopAddy": "456 Stop Addy"
            },
            {
                "StopAddy": "789 Stop Addy"
            }
        ]
    }
]
}'

Respondido 02 Feb 12, 03:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.