-1

I am creating C# application which gets the response from the web services and returns it as the List of DTO object like

 List<Models.iLabDTO.RootObject> response = new List<Models.iLabDTO.RootObject>();
 public async Task<IHttpActionResult> Get()
    {
      ......
      var request_returnDataObj = JsonConvert.DeserializeObject<Models.iLabDTO.RootObject>(responsefile);
      response.Add(request_returnDataObj);
      }
    return Ok(response);

But it throws error like

 <Error>
  <Message>An error has occurred.</Message>
 <ExceptionMessage>
 Error converting value "on" to type 'System.Boolean'. Path 
'il_response.c_forms[0].fields[0].required', line 1, position 354.
</ExceptionMessage>
 <ExceptionType>Newtonsoft.Json.JsonSerializationException</ExceptionType>

If I have just return OK(response) without using response.Add(request_returnDataObj) it works fine. But I am unable return the list of objects

xyz
  • 531
  • 1
  • 10
  • 31
  • 2
    Looks like its having issues with the value for 'il_response.c_forms[0].fields[0].required' because its value is "on" vs something like "true". if you wanted to use on and off for boolean flags you would need to have a custom implementation of the JsonConverter class. See here, https://stackoverflow.com/questions/14524669/how-to-get-newtonsoft-to-deserialize-yes-and-no-to-boolean – Bearcat9425 Oct 19 '17 at 20:18
  • Possibly related: https://stackoverflow.com/questions/6284936/why-is-a-checkbox-posting-a-value-of-on – Ben Cottrell Oct 19 '17 at 20:37
  • If you add more information it would be easier to help. It seems that sending your `var response = new List();` back to the server contains a property that contains the value `on` instead of `true` or `false` hence the message `"on" to type 'System.Boolean'`. What are the properties and type of `RootObject`. If you look at the response and compare the properties and values you will find out more. – surfmuggle Oct 19 '17 at 21:31

1 Answers1

0

The error says it's trying to convert "on" to a boolean, which it can't do. I can only guess that it's a field that is "on" or "off", hence the boolean field.

If you have control of the data you may want to change it to true and false. Other wise you have to write a JASON converter, but it will then treat all on's and off's as if they were a boolean type when you use the converter to deserialize. That may be a problem depending on what you are doing.

I had a similar problem here you can check out. At the bottom, in the last answer, I pasted some running code with an example of how to do it. You can cut and paste that in a console program and test it.

I hope that helps.

CodeChops
  • 1,980
  • 1
  • 20
  • 27