[SalesForce] String replace double quotes & slash

I'm trying JSON Parser and in the JSON I've \" (for like <tag style=\"color: #fff;\"...)
If I can replace \" with \\" the parser works fine (like in this – https://developer.salesforce.com/forums/?id=906F000000092v4IAA)

Input {"name":"Surya\".\""} like name = Surya"."

Output {"name":"Surya\".\""}

String input = '{"name":"Surya\".\""}';

System.debug( input );
//String output = input.replace('\"', '\\\"');
String output = input.replaceAll('\\\"', '\\\\\"');
System.debug( output );

I tried with replace & replaceAll.
Please advice me. Thanks.

Best Answer

If one of the value strings in your JSON is encoded, the simplest and most reliable way to decode is to use a deserialize call:

System.debug(JSON.deserializeUntyped('"Surya\\".\\""'));

For your example something like:

String input = '{"name":"Surya\".\""}';
Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(input);

String encodedNameValue = (String) m.get('name');
String nameValue = (String) JSON.deserializeUntyped(encodedNameValue);