1

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.

asked Mar 1, 2017 at 6:38
4
  • json2csharp.com Commented Mar 1, 2017 at 6:40
  • You don't even write how your JSON file looks like?!? Commented Mar 1, 2017 at 6:41
  • Perhaps your httpResponse is empty Commented Mar 1, 2017 at 6:41
  • @HristoYankov. No It isn't. I have mentioned in the comments, what I get in result. Commented Mar 1, 2017 at 6:45

2 Answers 2

1

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.

answered Mar 1, 2017 at 7:00

3 Comments

That is right. the result from HTTP is not actually JSON, and I dont have control over it.
What a pity. Then the quick fix will have to suffice.
I wanted to avoid doing if(result.Contains("success")). Thank you very much. that worked !!.
0

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"}
answered Mar 1, 2017 at 6:44

1 Comment

It gives a different error now Invalid property identifier character: `. On hovering result, the value is what you have mentioned. For Text Visualizer the value is {\"Status\":\"Success\"}` and for JSON visualizer it says, string is not in json format.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.