0

I'm using Json.NET to deserialize a jQuery.post() JSON param.

The raw value that's posted back is in the following format

jobs=4-5-6-7&invoiceDate=04-05-11

The class I'm trying to deserialize into is

public class InvoiceRequest
{
    public DateTime InvoiceDate { get; set; }
    public string JobList { get; set; }
}

And the code I'm using for this is

var sr = new System.IO.StreamReader(Request.InputStream);
var line = sr.ReadToEnd();
var deserializedProduct = JsonConvert.DeserializeObject<InvoiceRequest>(line);

The problem is that nothing happens when that third line is hit. When I step through the code, it reaches that line and then... nothing. The stepper disppears and the page never receives any response.

Can anyone explain what I'm doing wrong here?

awj
  • 7,482
  • 10
  • 66
  • 120
  • What is the value of `line`? If you are trying to deserialize `jobs=4-5-6-7&invoiceDate=04-05-11` - it won't work. That is not JSON format. It's a query string. JSON is something different, involving lots of quotes and curly braces~! It is usually passed as FORM/POST data, not in the URL as a query string. – Cheeso May 08 '11 at 16:20
  • 1
    ummmm thats not json, looks more like a form submission (key value pairs) – almog.ori May 08 '11 at 16:21
  • Also - If you are using WCF/REST or ASPNET/MVC2 on the server side, then you don't need to explicitly deserialize. It will be done for you, automatically. (If you're doing it right) – Cheeso May 08 '11 at 16:23
  • if your looking to post json to the server. id look here for a starter http://stackoverflow.com/questions/1184624/serialize-form-to-json-with-jquery – almog.ori May 08 '11 at 16:29
  • Ah, that's my embarrassing mistake. I'm using jQuery.post() with the "json" argument, thinking that this was posting in a JSON format. It turns out that this is the format of the response. Thanks for all your comments. – awj May 08 '11 at 17:05

2 Answers2

4

The following is application/x-www-form-urlencoded request, not JSON:

jobs=4-5-6-7&invoiceDate=04-05-11

If you want JSON the request should look like this:

{ 'jobs': '4-5-6-7', invoiceDate: '04-05-11' }
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3

It's not working because your data isn't JSON. Either change your JavaScript so that it sends data as JSON, or use HttpUtility.ParseQueryString to parse the format it's currently in.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159