0

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.

  • 1
    what is not working that is not clear from your question. – Santanu Boral Jul 16 '18 at 5:59
  • Please check the Input & Output and I'm trying few things which are not working for me. – Surya Chandra Rao Gandreddi Jul 16 '18 at 10:18
2

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);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.