如何编写Java代码,从JSON格式的代码中渲染出漂亮的彩色HTML呢?

huangapple go评论69阅读模式
英文:

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("&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\">\"</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 &quot;strings&quot;
int currentOption = 0;
for (char c : json.toCharArray()) {
if (c == &#39;\n&#39;) {
result.append(&quot;&lt;br/&gt;\n&quot;); 
} else if (c == &#39; &#39;) {
result.append(&quot;&amp;nbsp;&quot;);
} else if (Character.isAlphabetic(c) || Character.isDigit(c)) {
if (currentOption == 0) {
currentOption = 1;
result.append(&quot;&lt;span class=\&quot;markup_numbers\&quot;&gt;&quot;);
}
result.append(c);
} else if (c == &#39;,&#39; || c == &#39;[&#39; || c == &#39;]&#39; || c == &#39;{&#39; || c == &#39;}&#39; || c == &#39;:&#39;) {
if (currentOption == 1) {
currentOption = 0;
result.append(&quot;&lt;/span&gt;&quot;);
}
if (currentOption == 3) {
result.append(c);
} else {
result.append(&quot;&lt;span class=\&quot;markup_punct\&quot;&gt;&quot;).append(c).append(&quot;&lt;/span&gt;&quot;);
}
} else if (c == &#39;\&quot;&#39;) {
if (currentOption == 3) {
currentOption = 0;
result.append(&quot;\&quot;&lt;/span&gt;&quot;);
} else if (currentOption == 0) {
currentOption = 3;
result.append(&quot;&lt;span class=\&quot;markup_strings\&quot;&gt;\&quot;&quot;);
} else {
throw new RuntimeException(&quot;invalid json&quot;);
}
} else {
result.append(c);
}
}
return result.toString();
}

huangapple
  • 本文由 发表于 2020年9月28日 15:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64097355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定