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.

12
  • Are you sure currentTokenNo variable is declared as String ? Commented Jan 3, 2016 at 13:50
  • @miensol yes I'm sure !!! It's generated encrypted form of string.. Commented Jan 3, 2016 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 Commented Jan 8, 2016 at 4:12
  • it tries to escape the slashes which is legit. Commented Jan 8, 2016 at 4:15
  • @k0sh I know it is trying to escape characters but could not find out why quotation marks are added to It.. Commented Jan 8, 2016 at 4:18

4 Answers 4

2
+50

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!

Sign up to request clarification or add additional context in comments.

7 Comments

If so then how to save String to SharedPreferences without quatation marks.
If you can look at the SP's xml file, you will see something as the following <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="token">_bcfPG5ToPzecCGxrosgA6AKNHgdfieuKEXxmQZUz-9ACg</string> <string name="username">[email protected]</string> </map>
And I think you should check the original value of the token, I mean that you check the code that generates and/or reponses the token
Hi! Have you checked as I commented?
Yes I've. Will my temporary solution works for future then.
|
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

Comments

1

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.