0

Below is the result of my JSON:

{"imageSignature":"","serviceAmount":"0","regionCode":"SG","MCPTID":"90","brandName":"MasterCard"}

Now when i try to pass to JSONObject It Gives syntax Error ("Syntax Error on tokes,delete these tokens"):

JSONObject json = new JSONObject("{"imageSignature":"","serviceAmount":"0","regionCode":"SG","MCPTID":"90","brandName":"MasterCard"}");

It Works fine when i do ESCAPE manually:

JSONObject json = new JSONObject("{\"imageSignature\":\"\",\"serviceAmount\":\"0\",\"regionCode\":\"SG\",\"MCPTID\":\"90\",\"brandName\":\"MasterCard\"}");

SO can please someone tell me how can i pass the same to JSONObject() .??

hata
  • 11,633
  • 6
  • 46
  • 69
user2888996
  • 417
  • 8
  • 20
  • Why do you need the JSON string in your source code? Better put it in a file. – Thilo Oct 30 '15 at 05:28
  • http://stackoverflow.com/questions/6068197/utils-read-resource-text-file-to-string-java – Thilo Oct 30 '15 at 05:34
  • I still don't understand how a JSON **result** needs to be hard-coded (in source or resource). Isn't this String the result of a previous operation? – Thilo Oct 30 '15 at 05:38
  • @user2888996 What do you mean about JSON **result**? – hata Oct 30 '15 at 05:38
  • I am Writing Util Class which takes JSON data and the Parse it. My data is : { "imageSignature": "", "serviceAmount": "0" } – user2888996 Oct 30 '15 at 05:39

1 Answers1

0

Now when i try to pass to JSONObject It Gives syntax Error ("Syntax Error on tokes,delete these tokens"):

When you are manually creating String constant in java, you have to escape the double quotes.

It Works fine when i do ESCAPE manually:

Escaping the double quotes with backslashes is the only way to do this in java.

You can also store the string in some file and read read and pass string object to JsonObject constructor

SO can please someone tell me how can i pass the same to JSONObject()

Escaping the double quotes with backslashes is the correct approach

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • Alternative to blackslashes: Use single quotes in the String literal and then call `replace` on it (assuming there are no single quotes in the literal itself). – Thilo Oct 30 '15 at 05:29
  • @Thilo though of the same, but names of people and other text might contain single quotes, just to be on a safer side, we can go with above. :) – Ankur Singhal Oct 30 '15 at 05:32
  • @ankur-singhal I have to keep JSON result to file and then read .?? – user2888996 Oct 30 '15 at 05:32
  • Also, single quote needs to quoted itself (in the replace). One could use `*` and `replace('*', '"')`. With a literal, you know what character is "unused". But best to put in a text file, as you say. – Thilo Oct 30 '15 at 05:33
  • 2
    @user2888996 then put it inside text file, read and pass `string object` to `JsonObject` constructor – Ankur Singhal Oct 30 '15 at 05:35
  • @Thilo All Your solution (replace) will work when I am able to pass JSON result. But as i said It given Syntax Error Even you pass into new String(""), So basically I want to know How can i pass my JSON result directly to any of the functions? – user2888996 Oct 30 '15 at 05:37
  • @user2888996 better you post your complete program, somewhere you are messing up with the code. – Ankur Singhal Oct 30 '15 at 05:39
  • @ankur-singhal Expecting JSON result: { "imageSignature": "", "serviceAmount": "0" } Now i want to pass to Sting : String str =new String ("Above result"); I think it is clear now – user2888996 Oct 30 '15 at 05:44
  • @user2888996 above is very much parseable after escaping, you still have not posted the complete code, your utility class etc – Ankur Singhal Oct 30 '15 at 05:47