Reputation: 2523
Following a few solutions here, I chose to use NewtonSoft. But I'm unable to convert JSON to a class object. And I think, the json (or string) being passed to the method in not in the correct format.
class:
public class EmailAPIResult
{
public string Status { get; set; }
}
method:
//....code
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
string result = streamReader.ReadToEnd();
//when hovered on "result", the value is "\"{\\\"Status\\\":\\\"Success\\\"}\""
//For Text Visualizer, the value is "{\"Status\":\"Success\"}"
//And for JSON Visualizer, the value is [JSON]:"{"Status":"Success"}"
EmailAPIResult earesult = JsonConvert.DeserializeObject<EmailAPIResult>(result);
//ERROR : Error converting value "{"Status":"Success"}" to type 'EmailAPIResult'.
}
The following code works:
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
string good = "{\"Status\":\"Success\"}";
//when hovered on "good", the value is "{\"Status\":\"Success\"}"
//For Text Visualizer, the value is {"Status":"Success"}
//And for JSON Visualizer, the value is
// [JSON]
// Status:"Success"
EmailAPIResult earesult = JsonConvert.DeserializeObject<EmailAPIResult>(good); //successful
}
How should I format the "result", so that my code works.
Upvotes: 1
Views: 760
Reputation: 5920
As you've shown in your question :
when hovered on "result", the value is "\"{\\"Status\\":\\"Success\\"}\""
Which means your JSon string starts and ends with an escaped "
character.
Try to get rid of these like such ( for example ) :
string result = streamReader.ReadToEnd();
result = result.Substring(1);
result = result.Substring(0, result.Length - 1);
Which will give you the value like "{\\\"Status\\\":\\\"Success\\\"}"
EDIT :
Result I've posted was invalid ( my bad ) because it contains another unescaped character which is \
. You can get rid of this using string.Replace
method :
result = result.Replace("\\", string.Empty);
But the downside of these replacements would be that if your Json would contain this character, it would be replaced by empty (\0
) character
// {"Status":"Some\Other status"}
// would become
// {"Status":"SomeOther status"}
Upvotes: 0
Reputation: 9703
While it's true, that you can fix your string, like m.rogalski suggested, I'd recommend not to do it.
As you stated:
when hovered on "result", the value is "\"{\\\"Status\\\":\\\"Success\\\"}\""
I'd suggest to examine where this comes from. What is your backend implemented like? It seems as if the JSON result of your HTTP answer is not actually JSON, but a fully escaped JSON string. If you have the control over what is happening in the backend, you should really fix it, since you will end up with issues like that every time you try and write a client to your HTTP services.
Anyway, if you want the quick fix, try that one:
result = result.Replace(@"\\", @"\").Replace(@"\""", "\"").Trim('\"');
the calls to Replace
replaces the escaped characters with the unescaped ones. Trim
trims the leading and trailing quotes.
Upvotes: 1