awj
awj

Reputation: 7959

Json.NET - JsonConvert.DeserializeObject isn't doing anything

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?

Upvotes: 0

Views: 1378

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

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' }

Upvotes: 4

Matti Virkkunen
Matti Virkkunen

Reputation: 65166

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.

Upvotes: 3

Related Questions