0

The codes is like the following:

JSONObject solution = new JSONObject(); 
variableName = "TEST"                   
System.err.println("1:"+value);
solution.put(variableName, value);
System.err.println("2:"+solution);

Here is the output result:

1:{"min":10,"max":40}
2:{"TEST":"{\"min\":10,\"max\":40}"}

How can I get rid of the annoying '\'? Thank you very much!

xlm
  • 6,854
  • 14
  • 53
  • 55

1 Answers1

0

The reason why you are getting \ in your print line is because it is escaping the " character which is used in value. There's nothing wrong with this, it simply signifies that \" is not terminating the string and is instead a value part of that string.

Usage of double quotations is valid JSON, not single quotes - see related Q here.

But if you really want to, if you create value like this:

    JSONObject value = new JSONObject("{'min':10,'max':40}");       

Then you should get the desired output from your existing code:

1:{"min":10,"max":40}
2:{"TEST":{"min":10,"max":40}}
Community
  • 1
  • 1
xlm
  • 6,854
  • 14
  • 53
  • 55