vb.net의 JSON.NET 3.5B2의 클라이언트에서 JSON을 사용하여 Deserializing

StackOverflow https://stackoverflow.com/questions/489723

  •  20-08-2019
  •  | 
  •  

문제

내가 가진 문제의 테스트 사례가 있습니다. 기본적으로 서버에서 JSON 객체를 생성하고 클라이언트 측에서 만든 Ajax에 다시 제공합니다. 그러나 그것은 잘 작동하지만, 그것을 다시 보내고 IT 서버 측면에 사로화하려고 할 때 나는 다음과 같은 예외를 얻는다.

'System.String'유형에 대해 정의 된 매개 변수없는 생성자가 없습니다.

system.web.script.serialization.objectconverter.convertDictionaryToBject (idictionary2 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(IDictionary2 rawparams)에서 system.web.script.services.webservicemethoddata.callmethodfromrawparams (객체 대상, 유명2 parameters) at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary2 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

내가 여기서 잘못하고있는 일에 대한 도움을 주셔서 감사합니다. 미리 감사드립니다.

도움이 되었습니까?

해결책

변경해야했습니다

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top