I am trying to send a custom payload to a String in Java. In this case, a JSON Object.
Ordinarily, if I wanted to send a decimal to a string I might do something like this
String myDecimal = "my decimal is %d";
System.out.println(String.format(myDecimal, 20)); // print "my decimal is 20)
However, instead of sending a decimal, or a string (%s), I would like to send a JSON object. Is this possible, if so how?
As far as I know, you cannot pass an object. so far, my best solution has been passing strings formatted to be like a JSON object, then parsing it into a JSON Object.
Example
class Main {
private static String EXT_EVENT = "{" +
" \"version\": \"1.0\"," +
" \"method\": \"Event\"," +
" \"target\": \"ExamppleTarget\"," +
" \"name\": \"OnEventReceived\"," +
" \"payload\": { \"header\": %1$s," +
" \"endpoint\": %2$s, " +
" \"payload\": %3$s }" +
"}";
public static void main(String[] args) {
String example = String.format(EXT_EVENT,"test1", "test2", "test3");
System.out.println(example);
}
}