Home > Mobile >  How to correctly generate Json without Backslashes
How to correctly generate Json without Backslashes

Time:05-20

Problem:

I get json with backslashes as response from a GET Request

@RequestMapping(value = "repuve/recibe_captcha", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)

like this:

{"mensaje":"Proceso ejecutado correctamente.","folio":"1652989601959","resultado":"{\"idSalida\":1,\"descSalida\":\"Proceso Correcto\",\"data\":[.....]}"}

When expected Result is like this:

{"mensaje":"Proceso ejecutado correctamente.","folio":"1652989601959","resultado":"{"idSalida":1,"descSalida":"Proceso Correcto","data":[.....]}"}

(The data array has a b64 image).

This is how json is created:

JSONObject object = new JSONObject();
JSONArray jArray = new JSONArray();
object.put("idSalida", 1);
object.put("descSalida", "Proceso Correcto");
jArray.put(captcha);
object.put("data", jArray);

I've tried some answers from StackOverflow using replace() but none of them seems to work for me.

Is there another way to remove the backslashes and get the correct json format delivered to front?

Thank you.

CodePudding user response:

Any JSON generating framework should actually escape the nested quotation marks. If you expect the string to look differently, you will have write code that assembles that JSONary string the way you want.

  • Related