8

I have a Json string like below

 String jsonRequestString = "{\"access_code\" : \"9bPbN3\" , "
                          + "\"merchant_reference\" : \"123\", \"language\" : \"en\",\"id\" : \"149018273\","
                          + "\"merchant_identifier\" : \"gKc\", \"signature\" : \"570fd712af47995468550bec2655d9e23cdb451d\", "
                          + "\"command\" : \"VOID\"}";

I have a String variable as

String code = "9bPbN3";

Question, how do I plugin the above string instead of hardcoding it at the below place. i.e. instead of 9bPbN3, I want to use the variable code there.

   String jsonRequestString = "{\"access_code\" : \"9bPbN3\" , "

Many Thanks in advance.

5
  • This is about (very basic) string concatenation operation. You can combine a string literal with a variable like someString = "This is my value: " + variable
    – devnull69
    Mar 22, 2017 at 12:04
  • Thanks. Do I need to consider the : and the \ as well or, just something like: "{\"access_code\" +code , " Mar 22, 2017 at 12:06
  • You should wrap the variable in quotes if you'd like to pass it as a string: "{\"access_code\": \"" +code + "\"," Mar 22, 2017 at 12:10
  • 3
    I'd recommend using a JSON library to create and parse the JSON strings to reduce error-proneness especially if you're going to process critical production data.
    – das Keks
    Mar 22, 2017 at 12:10
  • Thank you so much guys. It works fine now. Thanks again. Mar 22, 2017 at 12:14

4 Answers 4

9

If you are struggling to arrange the "'s the correct syntax would be

String jsonRequestString = "{\"access_code\" : \""+code+"\" , ";

Instead of formatting Json string manually, which takes alot of effort, consider using a library or util.

For ex (going to use Jackson library) :

Request re = new Request();
re.setCode(code);
...
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(re);
6
String yourVariable = "xyz";
String jsonRequestString = "{\"access_code\" : \"" + yourVariable + "\" , "
                      + "\"merchant_reference\" : \"123\", \"language\" : \"en\",\"id\" : \"149018273\","
                      + "\"merchant_identifier\" : \"gKc\", \"signature\" : \"570fd712af47995468550bec2655d9e23cdb451d\", "
                      + "\"command\" : \"VOID\"}";
2
  • 1
    you should use "{\"access_code\" : \"" + yourVariable + "\"... =)
    – Erick Maia
    Mar 22, 2017 at 12:11
  • 1
    Erick, yeah, I just caught my mistaken quotation marks. Thanks. Mar 22, 2017 at 12:13
3

General advice is to avoid crafting a json structure out of vanilla strings. Instead use a json parser/writer library for this operations.

Checkout http://stleary.github.io/JSON-java/index.html / http://stleary.github.io/JSON-java/index.html .

There a various other libraries and tutorials available.

If you don't want to go this direction, use a "known value" placeholder and substitute it. So the full json would contain "access_code" : "@@ACCESS_CODE@@" and you would Substitute the placeholder with the real value. So your json string would be some kind of a string template.

1
  • 3
    IMO, this should be a comment, not an answer as this isn't really an answer to the question, just advice to not do what he's doing
    – Jerfov2
    Mar 22, 2017 at 12:14
1

Another option would be to use the format method like so:

 String jsonRequestString = "{\"access_code\" : \"%s\" , "
                          + "\"merchant_reference\" : \"123\", \"language\" : \"en\",\"id\" : \"149018273\","
                          + "\"merchant_identifier\" : \"gKc\", \"signature\" : \"570fd712af47995468550bec2655d9e23cdb451d\", "
                          + "\"command\" : \"VOID\"}";
String code = "9bPbN3";
String result = String.format(jsonRequestString, code);

Notice the "%s" I put in the place of where code would go. When you call the format method with code as a parameter, it puts it where the "%s" was.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.