7

This is my String

String currentTokenNo = "/SUeSjUf0A0aLFr+wVIZbw==\nLmmWtgHZ90yH0NBoATYB/A"

I've Added This String to my JsonObject which is a class of com.google.gson.JsonObject package

  JsonObject jsonToSubmit = new JsonObject();

    try {

        jsonToSubmit.addProperty("token", currentTokenNo);

      } catch (Exception e) {

        e.printStackTrace();
     }

But when I Log my String and JsonObject

Log.d("mainactivityjson", "Current Token No : "+currentTokenNo );
Log.d("mainactivityjson", "jsonToSubmit : "+jsonToSubmit);

I found the result

D/mainactivityjson: Current Token No : "/SUeSjUf0A0aLFr+wVIZbw==\nLmmWtgHZ90yH0NBoATYB/A"
D/mainactivityjson: jsonToSubmit : {"token":"\"/SUeSjUf0A0aLFr+wVIZbw==\\nLmmWtgHZ90yH0NBoATYB/A\""}

Now, My question is :

Why those quotation marks and slashes are added to value of JsonObject? Is there any suitable reason of it ?

It is really hampering the process of checking String's value at server side.

I've Done a temporary solution to accomplish the task by trimming single character from both side like following

jsonToSubmit.addProperty("token",currentTokenNo.substring(1,currentTokenNo.length()-1));

This worked Perfectly But I don't think It's a better idea for future !!!

Additional Information :

Variable currentTokenNo is not declared directly as shown above, It was retrieved from SharedPreferences & If it is declared directly then everything works fine.

String currentTokenNo = preferences.getString(LOGINCRED_TOKEN,"");

If the same variable is declared directly, Everything works fine.

Any kind of help will be appreciated.

Shree Krishna
  • 8,474
  • 6
  • 40
  • 68
  • Are you sure `currentTokenNo` variable is declared as `String` ? – miensol Jan 03 '16 at 13:50
  • @miensol yes I'm sure !!! It's generated encrypted form of string.. – Shree Krishna Jan 03 '16 at 13:54
  • What kind of string type are you passing? Do char, const char, something like that? Also do you have any encoding like UTF8 or dorm thing pointed on it anywhere? Know that's a weird question just getting more of an understanding of what you're passing – iSkore Jan 08 '16 at 04:12
  • it tries to escape the slashes which is legit. – Kosh Jan 08 '16 at 04:15
  • @k0sh I know it is trying to escape characters but could not find out why quotation marks are added to It.. – Shree Krishna Jan 08 '16 at 04:18
  • My logcat when using your code: `01-07 23:18:21.490 1619-1619/com.example.gsonitunes D/mainactivityjson: Current Token No : /SUeSjUf0A0aLFr+wVIZbw== LmmWtgHZ90yH0NBoATYB/A 01-07 23:18:21.490 1619-1619/com.example.gsonitunes D/mainactivityjson: jsonToSubmit : {"token":"/SUeSjUf0A0aLFr+wVIZbw==\nLmmWtgHZ90yH0NBoATYB/A"}` – BNK Jan 08 '16 at 04:19
  • @BNK so that I am telling if we declare string directly everything works fine brother. .. – Shree Krishna Jan 08 '16 at 04:21
  • Can you post logcat by using `Log.d("mainactivityjson", "Current Token No : "+currentTokenNo ); `right after `String currentTokenNo = preferences.getString(LOGINCRED_TOKEN,"");`? – BNK Jan 08 '16 at 04:23
  • @BNK `Current Token No "/SUeSjUf0A0aLFr+wVIZbw==y9iWl1SSrUe9zRQppENMNA"`.. Sorry for previous Token No. It was replaced. But problem appears only after adding it to `jsonObject`. – Shree Krishna Jan 08 '16 at 04:34
  • As you can see, the value from SharedPreferences has had quotation marks already, so if directly used, it must be `String currentTokenNo = "\"/SUeSjUf0A0aLFr+wVIZbw==y9iWl1SSrUe9zRQppENMNA\"";` – BNK Jan 08 '16 at 04:41
  • You can see in my previous comment `Current Token No : /SUeSjUf0A0aLFr+wVIZbw== LmmWtgHZ90yH0NBoATYB/A` (no quotation mark) :) – BNK Jan 08 '16 at 04:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100125/discussion-between-shree-krishna-and-bnk). – Shree Krishna Jan 08 '16 at 08:39

4 Answers4

2

From your comments and logcat info, the actual value getting from SharedPreferences is "/SUeSjUf0A0aLFr+wVIZbw==y9iWl1SSrUe9zRQppENMNA" (having quotation marks), not /SUeSjUf0A0aLFr+wVIZbw==y9iWl1SSrUe9zRQppENMNA. That's why you got jsonToSubmit : {"token":"\"/SUeSjUf0A0aLFr+wVIZbw==y9iWl1SSrUe9zRQppENMNA\""}


UPDATE:

For your temporary solution: jsonToSubmit.addProperty("token",currentTokenNo.substring(1,currentTokenNo.length()-1));

I prefer the following way:

jsonToSubmit.addProperty("token", currentTokenNo.replaceAll("\"",""));

If you also want to remove slashes, then use:

jsonToSubmit.addProperty("token", currentTokenNo.replaceAll("\"","").replaceAll("/",""));

Hope it helps!

BNK
  • 23,994
  • 8
  • 77
  • 87
2

Whatever you get in your logcat is correct.

Below are few points and reason why that happen so:

1) Unnecessary slashes added to your token. Why? Because your token contains back slash (\) that is an escaping character. So to it will be written as double slash (\\) instead of single slash (\).

2) Unnecessary quotation mark added to your token. Why? Again, your token is an String object and starts with a quotation ("). So it will be written as \".

So the overall you token changes from "/SUeSjUf0A0aLFr+wVIZbw==\nLmmWtgHZ90yH0NBoATYB/A" to "\"/SUeSjUf0A0aLFr+wVIZbw==\\nLmmWtgHZ90yH0NBoATYB/A\"". But you need not to worry as whenever you will get your token data from JsonObject, you will always get the same token which you were added.

For more info about escape character, see here: https://docs.oracle.com/javase/tutorial/java/data/characters.html

Geeky Singh
  • 1,003
  • 9
  • 20
0

It is some time after you posted this, but why not use org.json.JSONObject instead?

You wouldn't need to do that troublesome removal of unnecessary quotation marks and slashes removal.

Except if you try to do something like adding yourJsonObject.toString()into another JSONObject.

Gustavo Morales
  • 2,614
  • 9
  • 29
  • 37
Divole
  • 101
  • 1
  • 5
0

I was able to get past this problem by changing how the originating string was created, using getAsString() instead of toString() when pulling the string from another JsonObject.

When debugging, the String would quote as expected until inserted into the new JsonObject. Using the substring method above worked until I found to use getAsString().

String ref = jsonObject.get("_ref").toString();  // ""\"url.local\"""

whereas

String ref = jsonObject.get("_ref").getAsString();  // ""url.local""

Adding in case it helps someone searching this old issue.

rawslaw
  • 11
  • 1