3

I am working on application in which I need to pass all the data send to server via json array at exact user had passed.

But My problem is When I pass Single quote message doesn't send to server.

I had tried.

text=text.replaceAll("'","\'");

But My problem is

  1. When user press single quote message doesn't send.
  2. When user press single quote two time only one single quote is send.

Please give solution for that.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pooja Roy
  • 545
  • 5
  • 25

1 Answers1

2

Change your replaceAll("'","\'"); to replaceAll("'","\\u0027");

EDIT:

\u0027 is the unicode representation of '. Except in rare cases, unicode representations can be used in place of troublesome characters like ',",@,?, and &.

You are likely losing your single quote characters because your json is going through multiple layers of serialization/deserialization that are outside your control.

4mnes7y
  • 158
  • 8
  • 1
    Can you explain why that would help? – mabi May 22 '14 at 16:07
  • Edited the answer to include. Thank you @mabi for the suggestion; I may have written the original answer in haste. – 4mnes7y May 22 '14 at 17:03
  • 1
    Thanks, much better. That said, UTF-8 sequences are replaced at compile time (refer to JLS 3.3), so the runtime effect would be all the same. – mabi May 22 '14 at 17:09
  • @mabi You are correct, so using UFT-8 in pre-compiled code would be virtually meaningless. The question targets JSON, however, which is by nature not going to interact with the compiler. Using Unicode substitutions is standard practice when serving JSON responses between applications. – 4mnes7y May 22 '14 at 17:14
  • Hm? The example code from the OP indicates he's using Java to transform some input. Your suggested (unicode escape) snippet compiles to the exact same bytecode used and marked as not solving the OPs problem. – mabi May 22 '14 at 17:16
  • He is transforming input, in the form of a literal string, at runtime. If you disagree with my assessment, feel free to write your own test program to determine whether it behaves the way I say or not. Or, if it is more fitting to your taste, write your own answer and allow the OP to choose. Nothing would give me more joy than seeing another well-explained answer to this conundrum. – 4mnes7y May 22 '14 at 17:27
  • As I said, (and I am legitimately not being sarcastic here), write another answer. If you can provide a working solution, we are all better off for it. I'll admit that most of my experience with JSON has been in Ruby, Perl, JavaScript, and C#, with only a few months working with Java. I would appreciate an opportunity to learn how to work around this (overly annoying and totally unnecessary) problem... – 4mnes7y May 22 '14 at 17:44