SHORT ANSWER: first you need to deserialize the escaped string, but not into the target CLR type, but deserialize to another string:
// Initial example json string: "\"{\\\"Property1\\\":1988,\\\"Property2\\\":\\\"Some data :D\\\"}\"" // First, deserialize to another string (unescaped string). string unescapedJsonString = JsonConvert.DeserializeObject<string>(escapedJsonString); Debug.WriteLine(unescapedJsonString); // Prints: // "{\"Property1\":1988,\"Property2\":\"Some data :D\"}" // Second, deserialize to another string, again (in this case is necessary) var finalUnescapedJsonString = JsonConvert.DeserializeObject<string>(unescapedJsonString); Debug.WriteLine(finalUnescapedJsonString); // This time prints a final, unescaped, json string: // {"Property1":1988,"Property2":"Some data :D"} // Finally, perform final deserialization to the target type, using the last unescaped string. MyClass targetObject = JsonConvert.DeserializeObject<MyClass>(finalUnescapedJsonString);
FULL ANSWER (but interesting) Using string.Replace(... can generate an invalid string because it can damage certain special characters that require proper backslash deserialization.
This type of escaped string is usually generated when a string that was already a json string is serialized again (or even more times). This causes something like "different levels of serialization" (actually it is a serialization of a string with reserved characters), and the result is backslash characters (or groups of one, two or more backslashes follow: \, \\, \\\) are scattered throughout the line. Thus, to properly remove them, it is not enough to replace them with empty ones.
CORRECT WAY: The best way to get an unescaped string is to do the first deserialization for the string type (repeat this several times if necessary) and then do the final deserialization for the target CLR type:
// -- SERIALIZATION -- // Initial object MyClass originObj = new MyClass { Property1 = 1988, Property2 = "Some data :D" }; // "First level" Of serialization. string jsonString = JsonConvert.SerializeObject(originObj); Debug.WriteLine(jsonString); // Prints: // {"Property1":1988,"Property2":"Some data :D"} // "Second level" of serialization. string escapedJsonString = JsonConvert.SerializeObject(jsonString); Debug.WriteLine(escapedJsonString); // "{\"Property1\":1988,\"Property2\":\"Some data :D\"}" // Note the initial and final " character and de backslash characters // ... // at this point you could do more serializations ("More levels"), Obtaining as a result more and more backslash followed, // something like this: // "\"{\\\"Property1\\\":1988,\\\"Property2\\\":\\\"Some data :D\\\"}\"" // Note that is... very very crazy :D // ... // -- DESERIALIZATION -- // First deserialize to another string (unescaped string). string unescapedJsonString = JsonConvert.DeserializeObject<string>(escapedJsonString); Debug.WriteLine(unescapedJsonString); // Prints: // {"Property1":1988,"Property2":"Some data :D"} // ... // at this point you could repeat more deserializations to string, if necessary. For example if you have many backslash \\\ // ... // Finally, perform final deserialization to the target type, using the last unescaped string. MyClass targetObject = JsonConvert.DeserializeObject<MyClass>(unescapedJsonString);
Johan alzate
source share