gpt4 book ai didi

java - 如何格式化这个 Json 以添加额外的空格并使其看起来可读?

转载 作者:行者123 更新时间:2023-12-01 10:01:44 25 4
gpt4 key购买 nike

 {"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);,但它放置了一些我不需要的不必要的 block 。

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 的警告:

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

代码示例:

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

关于java - 如何格式化这个 Json 以添加额外的空格并使其看起来可读?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36766150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com