0
 {"question":"Gli elementi della porta nel calcio sono pali, la
 traversa e…","includeInfo: ":false,"info: ":"info:
 ","answers":{"answerText":"Porta","correct: ":"0"}} {"answers:
 ":{"answerText":"Una palla","correct: ":"0"}} {"answers:
 ":{"answerText":"La rete","correct: ":"1"}

I already used .writerWithDefaultPrettyPrinter().writeValueAsString(j);, but it puts some unnecessary blocks, which I don`t need.

I use a resultSet to extract data from my DB and that data consists of questions and answers basically, but each question has different number of answers. The point is to make a readable Json with the questions and the proper number of answers. And, finally, the problem is that the data is quite large and prerryPrinting recognise the type of my data(like boolean, String, chars) and puts it before the element. If I`m using the basic JsonObject, the data looks is shown above. Is there any other way to construct my Json?
  • 1
    Possible duplicate of [Custom pretty printer using Jackson library](http://stackoverflow.com/questions/18098513/custom-pretty-printer-using-jackson-library) – sidgate Apr 21 '16 at 10:02
  • What library are you using? – Thilo Apr 21 '16 at 10:07
  • Basic org.json library – Voicu Mihai Catalin Apr 21 '16 at 10:12
  • All of the prettyprinting methods puts some blocks, for example ("chars" : "Gli elementi della porta nel calcio sono pali, la traversa e…", "string" : "Gli elementi della porta nel calcio sono pali, la traversa e…", "valueType" : "STRING") How can I get rid of "String", "value", unnecessary field? – Voicu Mihai Catalin Apr 21 '16 at 10:14
  • Can you post an example of the JSON formatted as you would like? – Mario Trucco Apr 21 '16 at 14:00
  • { "question": "How old is Madonna?", "includeInfo": true, "info": "Madonna Louise Ciccone is an American singer, songwriter, actress, and businesswoman.", "answers": [ { "answer": "50", "correct": 0 }, { "answer": "54", "correct": 0 }, { "answer": "56", "correct": 1 } ] } I hope it is shown with spaces – Voicu Mihai Catalin Apr 21 '16 at 14:18
  • Each element on a new line with spaces before – Voicu Mihai Catalin Apr 21 '16 at 14:21
  • Do you need it to be readable on the browser for a human eye? if yes, then you may consider using some browser add-on to format the coming JSON, like [JSONView](https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc?hl=en) for Chrome. – Ahmad Tawila Apr 21 '16 at 14:50

1 Answers1

2

You can use the JSONObject.toString(int indentFactor) method. It takes an int representing your desired indent factor.

Just a warning from the org.json java docs:

Warning: This method assumes that the data structure is acyclical.

Code sample:

        String in = "{\"question\":\"Gli elementi della porta nel calcio sono pali, la  traversa e…\",\"includeInfo: \":false,\"info: \":\"info:  \",\"answers\":{\"answerText\":\"Porta\",\"correct: \":\"0\"}} {\"answers:  \":{\"answerText\":\"Una palla\",\"correct: \":\"0\"}} {\"answers:  \":{\"answerText\":\"La rete\",\"correct: \":\"1\"}";
        JSONObject obj = new JSONObject(in);
        System.out.println(obj.toString(4));
Mario Trucco
  • 1,933
  • 3
  • 33
  • 45