1

I'm sending that kind of json to asp.net web api action.

{
  "keys": {
     "2a":["C",,,,,"0",,"0"]
  }
} 

This json is POST-ed exactly as it is in request body to my asp.net 4.5 web api 2 action.

[RoutePrefix("api/TImport")]
[Authorize]
public class TImportController : ApiController
{

    [Route("")]
    [HttpPost]
    public async Task<TImportResult> Post(TImportParameters parameters)
    {
        // parameters.Keys["2a"] got deserialized as array[3] {"C","0","0" }   :(( 
        return await new TImport().RunAsync(parameters);
    }

What happens is that the 2a is deserialized into array of 3 elements instead of 8 elements with null or empty elements 2 to 6 and 7.

What can I do to to desserialize skipped array elements as defaults (nulls or empty strings)?

9
  • 1
    which library are you using for deserializing? Newtonsoft has special features like nullhandling and missingmemberhandling, may be worth checking Commented Jun 2, 2016 at 11:08
  • I'm using the standard library that is used for binding arguments in asp mvc. That is JsonMediaTypeFormatter based on json.codeplex.com I'm even unable to find out is that a reference behavior for json or a thing of particular library. Commented Jun 2, 2016 at 11:10
  • this is the newtonsoft one, check the property NullValueHandling in the JsonSerializer class you can set this to Include and try Commented Jun 2, 2016 at 11:13
  • how are you creating the object before sending? JSON.Net usually substitues null with null as default. 2a cannot be a property name of a C# object. Commented Jun 2, 2016 at 11:14
  • I don't the json is POST-ed in body to the asp.net web api action and there is a method parameter that it is auto binding to. I will update description. Commented Jun 2, 2016 at 11:18

1 Answer 1

1

that is an invalid json string. Though some json parsers ignore the bad syntax. And C#/javascript doesn't allow you to create properties that starts with numbers(2a in this case)[JSON.Net doesn't complain though]. A json string should contain key/values separated by comma(,).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.