Html Script Pass As String Into Json via to Java

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

Html Script Pass As String Into Json via to Java

问题

{ "content" : "

Some text

" }

Above mentioned pass html script into json via to java

英文:
{ "content" : "<html><body><p>Some text</p></body></html>" }

Above mentioned pass html script into json via to java

答案1

得分: 1

You need to use Jackson libraries for that.

如果你正在使用spring-boot,那么Jackson库已经包含在其中,用于JSON的消费和生成。

举例:

创建一个示例类。

public class HtmlContent {

  private String content;

  // getters and setters

}

Jackson代码:

public class Example {

     public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
         ObjectMapper objectMapper = new ObjectMapper();
         String json = "{\"content\" : \"<html><body><p>Some text</p></body></html>\" }";
         HtmlContent htmlContent = objectMapper.readValue(json, HtmlContent.class);
         System.out.println(htmlContent.getHtmlContent());
     }
}
英文:

You need to use Jackson libraries for that.

If you are using spring-boot, then jackson libraries are already included for JSON consumption and production.

For example :

Create a sample class.

public class HtmlContent {

  private String content;

  // getters and setters

}

Jackson code :

public class Example {

     public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
         ObjectMapper objectMapper = new ObjectMapper();
         String json = &quot;{ \&quot;content\&quot; : \&quot;&lt;html&gt;&lt;body&gt;&lt;p&gt;Some text&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;\&quot; }&quot;;
         HtmlContent htmlContent = objectMapper.readValue(json, HtmlContent.class);
         System.out.println(htmlContent.getHtmlContent());
     }
}

huangapple
  • 本文由 发表于 2020年8月3日 14:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63224733.html
匿名

发表评论

匿名网友

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

确定