0

Problem:

I am unable to parse json request to an object containing double quotes in it. For example:

jsonString = {
  "desc":"Hello stackOverFlow, please reach on this email "asdas@gmail.com". thanks";
}

When I am trying to convert this I am not able parse to an variable, because it looks like invalid json but in real time the request has double quotes in it.

Please show me some good parsing techniques which can do parse this type of requests.

Thanks

taygetos
  • 3,005
  • 2
  • 21
  • 29
Mr.K
  • 11
  • Well. This _is_ invalid JSON! See [How to escape double quotes in JSON](https://stackoverflow.com/questions/15637429/how-to-escape-double-quotes-in-json). – Seelenvirtuose Nov 25 '18 at 20:28
  • Thanks for you reply, the request I am getting is from another service which I don't have access to change that – Mr.K Nov 25 '18 at 20:59
  • Then ask them to correct their software! JSON is mainly used in software that should be written correctly. You can - however - write a function that correct the JSON before it goes to parsing. – Seelenvirtuose Nov 25 '18 at 21:01
  • can you please give some examples how to correct an invalid json, thanks for your patience. – Mr.K Nov 25 '18 at 21:08
  • Well ... Correcting that invalid JSON is not an easy task. There is no quick example. You have to develop it yourself. – Seelenvirtuose Nov 25 '18 at 21:10

1 Answers1

-1

You have to escape all of the double quotes, so for example:

String json = "\"{\"desc\":\"Hello stackOverFlow, please reach on this email \"asdas@gmail.com\". thanks\"}";

As you can see it is a lot of work to create very simple JSON, so you are better of using some library for that. I recommend you org.json, it is very lightweight and easy to use. Using it, it would look like this:

JSONObject json = new JSONObject();
json.put("description", "Your description....");
String jsonString = json.toString();
Binary Igor
  • 168
  • 1
  • 5