Note that there are some explanatory texts on larger screens.

plurals
  1. PODeserializing json from client in json.net 3.5b2 in vb.net
    text
    copied!<p>I have a test case of a problem I'm having, basically I create a json object on the server and provide it back to an AJAX called made on the client side. That works fine, however, when I try to send it back and deserialize it server side I get the following exception:</p> <blockquote> <p><strong>No parameterless constructor defined for type of 'System.String'.</strong></p> <p>System.MissingMethodException at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary<code>2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object&amp; convertedObject) at system.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object&amp; convertedObject) at system.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object&amp; convertedObject) at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer) at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary</code>2 rawParams) at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary<code>2 parameters) at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary</code>2 rawParams) at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)</p> </blockquote> <p><strong>Default.aspx</strong></p> <pre><code>&lt;%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;Untitled Page&lt;/title&gt; &lt;script src="http://www.json.org/json2.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="http://code.jquery.com/jquery-latest.pack.js" type="text/javascript" &gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(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); } }); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;input id="Button1" type="button" value="Get Date" /&gt;&lt;br /&gt; date.now: &lt;input type="text" id="now" value="" style="width:20em" /&gt;&lt;br /&gt; date.now processed: &lt;input type="text" id="pnow" value="" style="width:30em"&gt;&lt;br /&gt; date.strnow: &lt;input type="text" id="strnow" value="" style="width:20em"/&gt; &lt;input id="Button2" type="button" value="Send Date" /&gt;&lt;br /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Default.aspx.vb</strong></p> <pre><code>Imports System.Web.Services Imports Newtonsoft.Json Partial Class _Default Inherits System.Web.UI.Page Private Class _date &lt;JsonProperty()&gt; Public now As New Date &lt;JsonProperty()&gt; Public strnow As String Public Sub New() now = Date.Today strnow = Date.Today.ToLongDateString End Sub End Class &lt;WebMethod()&gt; Public Shared Function getDate() As String Dim theDate As New _date Dim strJson As String strJson = JsonConvert.SerializeObject(theDate) Return strJson End Function &lt;WebMethod()&gt; 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 </code></pre> <p>I'd greatly appreciate help on what I'm doing wrong here. Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload