在vb.net中的json.net 3.5b2中从客户端反序列化json

时间:2009-01-28 22:35:17

标签: vb.net jquery json.net

我有一个我遇到的问题的测试用例,基本上我在服务器上创建一个json对象并将其提供回客户端调用的AJAX。这工作正常,但是,当我尝试将其发回并反序列化服务器端时,我得到以下异常:

  

没有为'System.String'类型定义无参数构造函数。

     

System.MissingMethodException at   System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary的2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) at system.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) at system.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer) at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary 2   rawParams)at   System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(对象   target,IDictionary 2 parameters) at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary 2 rawParams)   在   System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext的   context,WebServiceMethodData   methodData)

Default.aspx的

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    <script src="http://www.json.org/json2.js" type="text/javascript"></script>
    <script src="http://code.jquery.com/jquery-latest.pack.js" type="text/javascript" ></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $.theDate = {};
            $("#Button1").click(function(){
                $.ajax({
                    type: 'POST',
                    url: 'default.aspx/getDate',
                    data: '{}',
                    contentType: 'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function(json){
                        var date = eval("("+json.d+")");
                        $.theDate = date;
                        var pdate = eval("("+json.d.replace(/"\\\/Date\(([0-9]+)[0-9-]+\)\\\/"/gi, 'new Date(parseInt($1,10))')+")");
                        $("#now").val(date.now);
                        $("#pnow").val(pdate.now);
                        $("#strnow").val(date.strnow);
                    },
                    error: function(xmlhr, status, err){
                        console.error(arguments);
                        var response = eval("("+xmlhr.responseText+")");
                        console.log(response);
                    }
                });
            });

            $("#Button2").click(function(){
                var json = JSON.stringify($.theDate);
                json = '{"json":'+json+'}';
                $.ajax({
                    type:'POST',
                    url:'default.aspx/deserialize',
                    data: json,
                    contentType:'application/json; charset=utf-8',
                    dataType: 'json',
                    success: function(json){
                        var date = eval("("+json.d+")");
                        console.log(date);
                    },
                    error: function(xmlhr, status, err){
                        console.error(arguments);
                        var response = eval("("+xmlhr.responseText+")");
                        console.log(response);
                    }
                });
            });
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="Get Date" /><br />
        date.now: <input type="text" id="now" value="" style="width:20em" /><br />
        date.now processed: <input type="text" id="pnow" value="" style="width:30em"><br />
        date.strnow: <input type="text" id="strnow" value=""  style="width:20em"/>
        <input id="Button2" type="button" value="Send Date" /><br />
    </div>
    </form>
</body>
</html>

Default.aspx.vb

Imports System.Web.Services
Imports Newtonsoft.Json

Partial Class _Default
    Inherits System.Web.UI.Page

    Private Class _date
        <JsonProperty()> Public now As New Date
        <JsonProperty()> Public strnow As String
        Public Sub New()
            now = Date.Today
            strnow = Date.Today.ToLongDateString
        End Sub
    End Class

    <WebMethod()> Public Shared Function getDate() As String
        Dim theDate As New _date
        Dim strJson As String

        strJson = JsonConvert.SerializeObject(theDate)

        Return strJson
    End Function

    <WebMethod()> Public Shared Function deserialize(ByVal json As String) As String
        Dim newDate As _date
        Try
            newDate = JsonConvert.DeserializeObject(json, GetType(_date))
            Return "{""done"":true}"
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class

我非常感谢帮助我在这里做错了什么。提前谢谢。

1 个答案:

答案 0 :(得分:3)

不得不改变

Private Class _date 

Public Class _date

<WebMethod()> Public Shared Function deserialize(ByVal json As String) As String

<WebMethod()> Public Shared Function deserialize(ByVal json As _date) As String