简体   繁体   中英

Newtonsoft.Json.JsonSerializationException in deserializing the JSON response C#

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM