簡體   English   中英

如何格式化這個Json來放置額外的空格並使它看起來可讀?

[英]How can I format this Json to put extra spaces and make it look readable?

 {"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"}

我已經使用了.writerWithDefaultPrettyPrinter().writeValueAsString(j); ,但會放置一些不必要的塊,這些塊是我不需要的。

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?

您可以使用JSONObject.toString(int indentFactor)方法。 它需要一個int表示所需的縮進因子。

只是來自org.json java docs的警告:

警告:此方法假定數據結構是非周期性的。

代碼示例:

        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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM