Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove escape characters from a JSON String

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;
    }
like image 507
Sai Wai Maung Avatar asked Sep 22 '14 17:09

Sai Wai Maung


People also ask

How remove all escape characters from JSON string?

replaceAll("\\","");

What is an escape character in JSON?

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.

How to escape invalid characters from a JSON field?

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.

What characters are reserved in JSON strings?

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 \\.

Is there an escape character in getperson() string?

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.


1 Answers

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);
like image 188
Sai Wai Maung Avatar answered Sep 30 '22 20:09

Sai Wai Maung