Search code examples
jsonsalesforceapex-code

APEX JSON Generator writeString escapes quotes


Is there a way NOT to escape the quotes in a string when using JSON Generator's method writeString? I'm getting the following result:

"{\"Name\":\"asdsads\",\"Query\":\"adasdasd\"},{\"Name\":\"12312312\",\"Query\":\"3123123\"},{\"Name\":\"d23d2\",\"Query\":\"3d23d2\"}"

instead of:

{"Name":"asdsads","Query":"adasdasd"},{"Name":"12312312","Query":"3123123"},{"Name":"d23d2","Query":"3d23d2"}

I have tried replace('\\', ''); as well as replace('\\"', '"'); but didn't work.

Any help is appreciated.


Solution

  • Solved it. Had to do the following:

    String genString = gen.getAsString();
    genString = genString.replace('\\"', '"');
    genString = genString.replace('"{', '{');
    genString = genString.replace('}"', '}');
    

    Replacing on the fly didn't work for some reason.