Click here to Skip to main content
15,896,268 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
In my app, I added several arrays into a new Array object, and then post the data through AJAX. In the myHandler, I want to deserialize the data into arrays. But I got failure in the deserialization. If you know how to for deserialize such an objective, I will appreciate if you can share your knowledge.
JavaScript
var arrays = new Array();
arrays.push(ar1);   // ar1 - Array
arrays.push(ar2);   // ar2 - Array but different from ar1 in dimension
arrays.push(ar3);   // ar3 - Array, also different in dimension
$.ajax({
    async: true,
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    type: "POST",
    data: { arrays },
    url: "myHandler.ashx",
    success: function (result) { }
});

C#
// code in myHandler.ashx
public void ProcessRequest(HttpContext context)  {
    context.Response.ContentType = "application/json";
    var data = context.Request;
    var sr = new StreamReader(data.InputStream);
    var stream = sr.ReadToEnd();
    var javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    vvar obj = javaScriptSerializer.DeserializeObject(stream);    // Got error here

Below is the value of stream:
C#
"ar%5B0%5D%5B%5D=8594&ar%5B0%5D%5B%5D=K02R019&ar%5B0%5D%5B%5D=K02&ar%5B0%5D%5B%5D=IB-K-29&ar%5B0%5D%5B%5D=TRACKWAY&ar%5B0%5D%5B%5D=PRIVATE&ar%5B0%5D%5B%5D=BRASS+DISK&ar%5B0%5D%5B%5D=&ar%5B0%5D%5B%5D=38.88722&ar%5B0%5D%5B%5D=-77.09436&ar%5B0%5D%5B%5D=K02&ar%5B1%5D%5B0%5D%5B%5D=5324&ar%5B1%5D%5B0%5D%5B%5D=8594&ar%5B1%5D%5B0%5D%5B%5D=222%2B54.45&ar%5B1%5D%5B0%5D%5B%5D=22254.45&ar%5B1%5D%5B0%5D%5B%5D=INBOUND&ar%5B1%5D%5B0%5D%5B%5D=&ar%5B1%5D%5B0%5D%5B%5D=&ar%5B1%5D%5B0%5D%5B%5D=POT&ar%5B2%5D%5B0%5D%5B%5D=8594&ar%5B2%5D%5B0%5D%5B%5D=8594&ar%5B2%5D%5B0%5D%5B%5D=K02R019&ar%5B2%5D%5B0%5D%5B%5D=&ar%5B2%5D%5B0%5D%5B%5D=SET&ar%5B2%5D%5B0%5D%5B%5D=5%2F30%2F1905&ar%5B2%5D%5B0%5D%5B%5D=DUFF&ar%5B2%5D%5B0%5D%5B%5D=ON+INVERT&ar%5B2%5D%5B1%5D%5B%5D=18059&ar%5B2%5D%5B1%5D%5B%5D=8594&ar%5B2%5D%5B1%5D%5B%5D=K02R019&ar%5B2%5D%5B1%5D%5B%5D=&ar%5B2%5D%5B1%5D%5B%5D=RECNORECORD&ar%5B2%5D%5B1%5D%5B%5D=&ar%5B2%5D%5B1%5D%5B%5D=&ar%5B2%5D%5B1%5D%5B%5D=null&ar%5B3%5D%5B0%5D%5B%5D=15015&ar%5B3%5D%5B0%5D%5B%5D=8594&ar%5B3%5D%5B0%5D%5B%5D=859
4&ar%5B3%5D%5B0%5D%5B%5D=&ar%5B3%5D%5B0%5D%5B%5D=NAD27VAN&ar%5B3%5D%5B0%5D%5B%5D=447504.445&ar%5B3%5D%5B0%5D%5B%5D=2400018.492&ar%5B3%5D%5B0%5D%5B%5D=null&ar%5B3%5D%5B1%5D%5B%5D=30701&ar%5B3%5D%5B1%5D%5B%5D=8594&ar%5B3%5D%5B1%5D%5B%5D=18059&ar%5B3%5D%5B1%5D%5B%5D=PGIS&ar%5B3%5D%5B1%5D%5B%5D=LDPNSRS2007&ar%5B3%5D%5B1%5D%5B%5D=169632.8118&ar%5B3%5D%5B1%5D%5B%5D=182628.1492&ar%5B3%5D%5B1%5D%5B%5D=null"


What I have tried:

How to Deserialize a complicate object into arrays?
Posted
Updated 28-Sep-16 6:43am
v4
Comments
[no name] 28-Sep-16 11:49am    
Why is it after all this time, you *still* think that people half way around the world can read your mind or see your screen?
Richard Deeming 28-Sep-16 12:16pm    
Click "Improve question" and add a code block containing the JSON you're trying to deserialize, and the full error message you're getting.

1 solution

The data you're posting is not valid JSON. Decoding the string reveals:
ar[0][]=8594&ar[0][]=K02R019&ar[0][]=K02&ar[0][]=IB-K-29&ar[0][]=TRACKWAY&...

That's the standard application/x-www-form-urlencoded format used to submit a form.

Change your AJAX call to:
JavaScript
$.ajax({
    async: true,
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    type: "POST",
    data: JSON.stringify(arrays),
    url: "myHandler.ashx",
    success: function (result) { }
});

You should then get a valid JSON string in your stream variable.
 
Share this answer
 
Comments
s yu 28-Sep-16 14:15pm    
Wonderful! The runtime error was gone w/ your code. Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900