1

The following code I have implemented in the wicket. It is printing the entire JSON data in a single line. Actually it should print in a JSON format. As of my understanding, the problem is with document.getElementById(''demo'').innerHTML={0}. Please correct my code if I did anything wrong.

And why (''demo'') is expecting two single quotes at starting and ending of Html id.

target.appendJavaScript(MessageFormat
                    .format("document.getElementById(''demo'').innerHTML={0}'", mapper.writerWithDefaultPrettyPrinter()
                                    .writeValueAsString(jsonDataProducer())));
Krishna
  • 35
  • 9
  • Maybe I'm misunderstanding, but JSON data is always in JSON format. –  Sep 18 '19 at 14:20
  • yes, it is in JSON format only. I'm asking for pretty print format. Not like entire JSON data printing in a single line. – Krishna Sep 18 '19 at 14:24
  • I don't understand why you use two single quotes with getElementById. And 'demo' should correspond to a
     tag if you want to display the prettified  JSON
    – Andrea Del Bene Sep 18 '19 at 14:28
  • @Andrea Del Bene sorry for the delay. In MessageFormat "A single quote itself must be represented by doubled single quotes ''. I got this from following link: https://stackoverflow.com/questions/189889/java-messageformat-how-can-i-insert-values-between-single-quotes – Krishna Sep 20 '19 at 12:55

1 Answers1

1

I got answer to my question.

target.appendJavaScript(MessageFormat
                        .format("document.getElementById(''demo'').innerHTML=JSON.stringify({0},null,2);", jsonDataProducer));

First I tried to print the pretty JSON with the following code. mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonDataProducer().toString()) But, javascript is taking it as a object When I assign it to the document.getElementById(''demo'').innerHTML. So, I used JSON.stringify({0},null,2). It will stringify the javascript object in prettified JSON format.

And in MessageFormat a pair of single quotes are treated as a single quote. From here I got MessageFormat single quote information. We can also read MessageFormat documentation.

Krishna
  • 35
  • 9