c# - Deserialization of an Ajax -
i have ajax webmethod.
from webmethod getdate() "var" returning json:
var json2 = "[{\"id\":1,\"datetime\":04/10/2017,\"col1\":1,\"col2\":2,\"col3\":3}]"
error code:
invalid object passed in, ':' or '}' expected. (23): [{"id":1,"datetime":04/10/2017,"col1":1,"col2":2,"col3":3}]
my jquery code seems right:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#<%= button1.clientid %>").click(function () { var mo1 = $("#<%= textbox1.clientid %>").val(); var dy1 = $("#<%= textbox2.clientid %>").val(); var yr1 = $("#<%= textbox3.clientid %>").val(); var data = { mo:mo1, dy:dy1, yr:yr1 }; var json1 = json.stringify(data); $.ajax ({ type: "post", contenttype: "application/json; charset=utf-8", datatype: "json", data: json1, url: "default.aspx/getdate", success: function (result) { $("#<%= textbox4.clientid %>").val(result.d); $("#<%= button2.clientid %>").trigger("click"); }, error: function (status, ex) { alert("error code: status: " + status + " ex: " + ex); } }); return false; }); }); </script>
where error occurs in deserialization
protected void btn2_click(object sender, eventargs e) { int oo = 0; string[,] dataout = new string[100, 9]; var json2 = textbox4.text; var serializer = new system.web.script.serialization.javascriptserializer(); list<data> data2 = serializer.deserialize<list<data>>(json2); foreach (var element in data2) { oo += 1; dataout[oo, 1] = convert.tostring(element.id); dataout[oo, 2] = convert.tostring(element.datetime); dataout[oo, 3] = convert.tostring(element.col1); dataout[oo, 4] = convert.tostring(element.col2); dataout[oo, 5] = convert.tostring(element.col3); } }
i tried newtonsoft json.net:
list<data> data2 = jsonconvert.deserializeobject<list<data>>(json2);
error code:
newtonsoft.json.jsonreaderexception occurred hresult=0x80131500 message=error parsing comment. expected: *, got 1. path '[0].datetime', line 1, position 23.
how deserialzer function correctly?
Comments
Post a Comment