C# client throwing exception trying to Deserialize a json response into a string
See original GitHub issueI am using an externally generated swagger.json definition that returns json data as a string. The client is failing with an exception calling DeserializeObject in the following code
if (status_ == "200")
{
var responseData_ = await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
var result_ = default(string);
try
{
result_ = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(responseData_, _settings.Value);
return result_;
}
catch (System.Exception exception_)
{
throw new SwaggerException("Could not deserialize the response body.", status_, responseData_, headers_, exception_);
}
}
The call to DeserializeObject fails (and is not required) when the result is also a string. The result should just be set to the reponseData value.
Whats the best way to accomplish this? Do I need to update templates?
Thanks.
Issue Analytics
- State:
- Created 6 years ago
- Comments:14 (4 by maintainers)
Top Results From Across the Web
Deserialize JSON to c# object raising an exception
The score property is sometimes holding float value but in my c# class, there is datatype int which causes the exception.
Read more >How to serialize and deserialize JSON using C# - .NET
Learn how to use the System.Text.Json namespace to serialize to and deserialize from JSON in .NET. Includes sample code.
Read more >SerializationException: There was an error deserializing ...
When I debug and place a checkpoint on the error (line 105) I get the 2 ... DeserializeObject() method to convert the json...
Read more >SyntaxError: JSON.parse: bad parsing - JavaScript | MDN
JSON.parse() parses a string as JSON. This string has to be valid JSON and will throw this error if incorrect syntax was encountered....
Read more >Sending and Receiving JSON using HttpClient with System ...
This will cause a HttpRequestException to be thrown when the response is not in the 200-299 status code range. The library code will...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
A string like
foobar
is not valid JSON, only"foobar"
ishttps://docs.microsoft.com/en-us/aspnet/core/web-api/advanced/formatting?view=aspnetcore-6.0
This can be fixed on the server side of the API