0

I have this json String right now:

private static final String JSON_REQUEST = "{ \"username\" : \"myusername\" ,\"password\" : \"mypassword\" , \"name\" : \"myname\"}";

Instead of myusername, mypassword, myname I want to add variables like this

String username = user.getText.toString;
String password = password.getText.toString;
String name= name.getText.toString;

3 Answers 3

1

Your question is ambiguous, but what i believe is, you want the values to be parameterized in which case you can do is,create a template like this

private static final String JSON_REQUEST_TEMPLATE = "{ \"username\" : \"%s\" ,\"password\" : \"%s\" , \"name\" : \"%s\"}";

and then replace the values with the values like

String JSON_REQUEST = String.format(JSON_REQUEST_TEMPLATE, user.getText.toString, password.getText.toString, name.getText.toString);

Hope that helps.

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

Comments

0

You will need to replace:

"username" with -> "+username+"

Likewise, to it for other fields too.

Comments

0

Use a String formatter

private static final String JSON_REQUEST_PATTERN = "{ \"username\" : \"%s\" ,\"password\" : \"%s\" , \"name\" : \"%s\"}";

String username = user.getText.toString;
String password = password.getText.toString;
String name= name.getText.toString;
String jsonRequest = String.format(JSON_REQUEST_PATTERN, username, password, name);

Or, in Kotlin

val jsonRequest = "{ \"username\" : \"$username\" ,\"password\" : \"$password\" , \"name\" : \"$name\"}"

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.