
java中json字符串与实体类对象相互转换
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json

·
1、问题描述
有一个需求是这样的,把实体类转为Json字符串存入redis中,然后再把redis中存放的实体类Json字符串插入数据库中。因此需要涉及到json字符串与实体类对象的相互转换。
2、产生原因
redis不支持直接存放java对象
因此需要用 JSON.toJSONString 把java对象转为json字符串。
3、解决方法
使用JSON.toJSONString 把java对象转为json字符串。
redisTemplate.opsForList().leftPushAll("notify:" + notifyMessageBO.getRecipienterId(), JSON.toJSONString(messageBO))
把json字符串转为java实体类对象
@Test
public void testJson() {
List<Object> list = redisTemplate.opsForList().range("notify:102", 0, -1);
System.out.println(list);
for (Object o : list) {
try {
JSONObject jsonObject = new JSONObject().parseObject(o.toString());
String title = jsonObject.getString("title");
String htmlContent = jsonObject.getString("htmlContent");
String beginTime = jsonObject.getString("beginTime");
Integer canMarkProcessstate = Integer.parseInt(jsonObject.getString("canMarkProcessstate"));
Integer processState = Integer.parseInt(jsonObject.getString("processState"));
MessageBO messageBO = new MessageBO(title, htmlContent, beginTime, canMarkProcessstate, processState);
System.out.println(messageBO);
} catch (JSONException jsonException) {
}
}
}
我使用的是阿里巴巴的fastjson
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
4、参考资料




适用于现代 C++ 的 JSON。
最近提交(Master分支:4 个月前 )
f06604fc
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
* :page_facing_up: bump the copyright years
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com>
---------
Signed-off-by: Niels Lohmann <mail@nlohmann.me>
Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 25 天前
d23291ba
* add a ci step for Json_Diagnostic_Positions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* Update ci.cmake to address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typo in the comment
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix typos in ci.cmake
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* invoke the new ci step from ubuntu.yml
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* address review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci check failures for unit-diagnostic-postions.cpp
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* improvements based on review comments
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix const correctness string
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* further refinements based on reviews
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add one more test case for full coverage
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* ci check fix - add const
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* add unit tests for json_diagnostic_postions only
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_diagnostics
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
* fix ci_test_build_documentation check
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
---------
Signed-off-by: Harinath Nampally <harinath922@gmail.com> 26 天前
更多推荐
所有评论(0)