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())));
4
  • Maybe I'm misunderstanding, but JSON data is always in JSON format. Commented Sep 18, 2019 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. Commented Sep 18, 2019 at 14:24
  • I don't understand why you use two single quotes with getElementById. And 'demo' should correspond to a <pre> tag if you want to display the prettified JSON Commented Sep 18, 2019 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: stackoverflow.com/questions/189889/… Commented Sep 20, 2019 at 12:55

1 Answer 1

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.

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

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.