英文:
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 = "{ \"content\" : \"<html><body><p>Some text</p></body></html>\" }";
HtmlContent htmlContent = objectMapper.readValue(json, HtmlContent.class);
System.out.println(htmlContent.getHtmlContent());
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论