英文:
How to write a Java code which renders nice looking colored HTML from JSON formatted code?
问题
对于我们的API,我们希望在一些专门用于展示API(例如销售页面)的页面上显示JSON示例。
因此,它需要被漂亮地格式化,使用带有颜色的标签。
我们过去可以使用JavaScript代码美化工具,但该项目已被存档且不再维护。
我们如何能够使用Java从美化后的JSON代码生成一个外观漂亮的HTML呢?
英文:
For our API, we would like on some pages dedicated to it (i.e. for sales) to display JSON examples.
So it needs to be nicely formatted, with span using colors.
We could use JavaScript code prettifier but that project is archived and no longer maintained.
How we could generate a nice looking HTML from beautified JSON code using Java?
答案1
得分: -1
以下是您要求的代码翻译部分:
public static String getHtmlFromButifiedJson(String json) {
StringBuilder result = new StringBuilder();
//0 - 无标记
//1 - 标记数字等
//2 - 标记标点符号
//3 - 标记"字符串"
int currentOption = 0;
for (char c : json.toCharArray()) {
if (c == '\n') {
result.append("<br/>\n");
} else if (c == ' ') {
result.append(" ");
} else if (Character.isAlphabetic(c) || Character.isDigit(c)) {
if (currentOption == 0) {
currentOption = 1;
result.append("<span class=\"markup_numbers\">");
}
result.append(c);
} else if (c == ',' || c == '[' || c == ']' || c == '{' || c == '}' || c == ':') {
if (currentOption == 1) {
currentOption = 0;
result.append("</span>");
}
if (currentOption == 3) {
result.append(c);
} else {
result.append("<span class=\"markup_punct\">").append(c).append("</span>");
}
} else if (c == '\"') {
if (currentOption == 3) {
currentOption = 0;
result.append("\"</span>");
} else if (currentOption == 0) {
currentOption = 3;
result.append("<span class=\"markup_strings\">\"</span>");
} else {
throw new RuntimeException("无效的 JSON");
}
} else {
result.append(c);
}
}
return result.toString();
}
请注意,代码中的类名 "markup_numbers"、"markup_punct" 和 "markup_strings" 都是假设的 CSS 类名,用于标记不同类型的内容。您可能需要根据实际需要进行适当的调整。
英文:
We did browse Stackoverflow and Google and couldn't find a solution. So we end up writing our own.
I have decided to post a code we did use here (for others as a reference):
public static String getHtmlFromButifiedJson(String json) {
StringBuilder result = new StringBuilder();
//0 - no markup
//1 - markup for numbers etc
//2 - markup for puncation
//3 - markup for "strings"
int currentOption = 0;
for (char c : json.toCharArray()) {
if (c == '\n') {
result.append("<br/>\n");
} else if (c == ' ') {
result.append("&nbsp;");
} else if (Character.isAlphabetic(c) || Character.isDigit(c)) {
if (currentOption == 0) {
currentOption = 1;
result.append("<span class=\"markup_numbers\">");
}
result.append(c);
} else if (c == ',' || c == '[' || c == ']' || c == '{' || c == '}' || c == ':') {
if (currentOption == 1) {
currentOption = 0;
result.append("</span>");
}
if (currentOption == 3) {
result.append(c);
} else {
result.append("<span class=\"markup_punct\">").append(c).append("</span>");
}
} else if (c == '\"') {
if (currentOption == 3) {
currentOption = 0;
result.append("\"</span>");
} else if (currentOption == 0) {
currentOption = 3;
result.append("<span class=\"markup_strings\">\"");
} else {
throw new RuntimeException("invalid json");
}
} else {
result.append(c);
}
}
return result.toString();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论