javascript - 是否可以在 java spring boot 中使用 json 类型?
问题描述
是否可以像在 javascript 中一样使用 json 类型而不在 java spring boot 中生成 JSONOject?是否可以在不创建 JSONObject 的情况下使用 { } 格式?
解决方案
JSON stands for "JavaScript Object Notation". The Java Syntax does not support JSON. Therefore, we cannot use JSON to declare any object in Java like
MyAwesomeObject object = { "name": "foo", "age": 42 };
We can, however, convert a String
that contains a JSON-string into a JSONObject
. See this question by dogbane for details.
If we use, for example, jackson, we can convert a String
to an object through jackson's ObjectMapper
:
final String json = "{ \"name\": \"foo\", \"age\": 42 }";
try {
MyAwesomeObject object = objectMapper.readValue(json, MyAwesomeObject.class);
} catch (IOException e) {
...
}
For more details on jackson, I recommend reading this tutorial from baeldung.com
.
推荐阅读
- javascript - JavaScript/Lodash:deepClone 似乎正在改变目标对象
- python - 在 python 中前滚数据,保持日期一致
- r - 代码逐行运行良好,但在 rmarkdown 中作为一个整体运行时失败
- r - 如何根据 R 中的多个条件将变量解析为多个列?
- python - 尝试通过 python 发送电子邮件并使用套接字出错
- java - 如何将字符串拆分为映射,使用流按重复键对值进行分组?
- javascript - 在 javascript 上使用 html 从 firebase 中删除数据
- c++ - VS Code C++ 重命名符号,跳过注释
- android-studio - Android Studio 在 KMP/KMM/KN 中添加了错误的 xcodeproj 位置
- php - 我无法安装支持 freetype 的 php [Ubuntu]