使用jackson json将属性添加到json字符串 [英] Add a property to a json string with jackson json

查看:1301
本文介绍了使用jackson json将属性添加到json字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将json字符串存储到mysql的文本字段中。
插入之后,我想更新我的json字符串并使用jackson json将mysql行id添加到其中。

I am storing a json string into a text field in mysql. After the insertion, i want to update my json string and add the mysql line id into it with jackson json.

我有一个java字符串,它在Json格式

I have a java String which is in Json format

{
  "thing":"val"
}

我想在不写代码行的情况下添加另一个K / V.

I'm looking to add another K/V without writing lines of codes.

最后得到这个:

{
  "thing":"val"
  "mysqlId":10
}

我可以将我的字符串转换为JsonNode:

I can convert my String to a JsonNode :

ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree( jsonStr);

希望做类似的事情

json.put("mysqlId",10);
json.toString();

然后在我的文本字段中使用mysql中的新json字符串进行更新

then update in my text field with new json string in mysql

我做不到。
我不想使用很多类是否有一个简单的方法与杰克逊这样做?

I can't make it. I don't want use many class is there a simple way to do so with jackson?

推荐答案

尝试铸造你的 JsonNode 到一个 com.fasterxml.jackson.databind.node.ObjectNode ,然后调用 put 设置(或替换)。

Try casting your JsonNode to an com.fasterxml.jackson.databind.node.ObjectNode and then calling put set (or replace) on it.

这篇关于使用jackson json将属性添加到json字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆