0

GSON is converting a string that contains a number into a number in Json. I'm using the following code to generate a JsonElement:

    JsonParser parser = new JsonParser();
    JsonElement sendPayload = parser.parse("{post_data : {login: "+login+", password: "+password+"}}");
    Log.d(CLASS_NAME, "Login Send payload: " + sendPayload);

It prints:

    LoginServiceManager﹕ Login Send payload: {"post_data":{"login":"[email protected]","password":123456}}

It should print:

    LoginServiceManager﹕ Login Send payload: {"post_data":{"login":"[email protected]","password":"123456"}}

How to achieve that?

1
  • What is the type of password Commented Apr 9, 2015 at 2:19

2 Answers 2

1

It looks like the bug is in your code that constructs a JSON string and passes this to parser.parse():

"{post_data : {login: [email protected], password: 123456}}"

That's not valid JSON so you're lucky or unlucky (depending on viewpoint) that parse() doesn't throw an exception.

A quick fix would be:

JsonElement sendPayload = parser.parse(
  "{\"post_data\": {\"login\": \"" + login + "\", \"password\": \"" + password + "\"}}");

A more robust fix would be to construct a JsonElement using its API. E.g. that wouldn't break if the password string contains ".

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

Comments

0

Hey You can try following workaround:

JsonElement sendPayload = parser.parse("{post_data : {login: "+login+", password:\""+password+"\"}}");

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.