I called a REST API with the following JSON string returned:
"{\"profile\":[{\"name\":\"city\",\"rowCount\":1,\"location\": ............
I tried to remove escape character with the following code before I deserialize it:
jsonString = jsonString.Replace(@"\", " ");
But then when I deserialize it, it throws an input string was not in a correct format
:
SearchRootObject obj = JsonConvert.DeserializeObject<SearchRootObject>(jsonString);
The following is the complete code:
public static SearchRootObject obj()
{
String url = Glare.searchUrl;
string jsonString = "";
// Create the web request
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
// Get response
var response = request.GetResponse();
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
jsonString = jsonString + readStream.ReadToEnd();
jsonString = jsonString.Replace(@"\", " ");
// A C# object representation of deserialized JSON string
SearchRootObject obj = JsonConvert.DeserializeObject<SearchRootObject>(jsonString);
return obj;
}
replaceAll("\\","");
Escapes or unescapes a JSON string removing traces of offending characters that could prevent parsing. The following characters are reserved in JSON and must be properly escaped to be used in strings: Backspace is replaced with b. Form feed is replaced with f. Newline is replaced with n.
The Description field I was passing has characters that JSON considers invalid characters. In order to use those characters they have to be escaped. typically with a backslash. Anyway, it was simple replace operation, although I used multiple compose.
The following characters are reserved in JSON and must be properly escaped to be used in strings: Backspace is replaced with \b. Form feed is replaced with \f. Newline is replaced with \n. Carriage return is replaced with \r. Tab is replaced with \t. Double quote is replaced with \". Backslash is replaced with \\.
However, the string has escape characters in it: public string GetPerson () { string person = repo.GetPerson (); //person is " {"name":jack,"age":"54"... return person; } When I try to view person in the text viewer when debugging, the escape characters are not there--Visual Studio rips them off.
After switching to use JavaScriptSerializer()
to deserialize JSON string , I realized that I have an int
type property
in my object for a decimal value in the JSON string. I changed int
to double
, and this solved my problem. Both JsonConvert.DeserializeObject<>
and JavaScriptSerializer()
handle escape character. There's no need to remove escape character.
I replaced the following codes:
jsonString = jsonString.Replace(@"\", " ");
SearchRootObject obj = JsonConvert.DeserializeObject<SearchRootObject>(jsonString);
return obj;
With:
return new JavaScriptSerializer().Deserialize<SearchObj.RootObject>(jsonString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With