javascript 使用 jackson json 将属性添加到 json 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16561989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a property to a json string with Hymanson json
提问by Dimitri Kopriwa
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 Hymanson json.
我正在将一个 json 字符串存储到 mysql 的文本字段中。插入后,我想更新我的 json 字符串并使用 Hymanson json 将 mysql 行 id 添加到其中。
I have a java String which is in Json format
我有一个 Json 格式的 java 字符串
{
"thing":"val"
}
I'm looking to add another K/V without writing lines of codes.
我希望在不编写代码行的情况下添加另一个 K/V。
to finally have this :
终于有了这个:
{
"thing":"val"
"mysqlId":10
}
I can convert my String to a JsonNode :
我可以将我的 String 转换为 JsonNode :
ObjectMapper mapper = new ObjectMapper();
JsonNode json = mapper.readTree( jsonStr);
Looking to do something like this
想做这样的事情
json.put("mysqlId",10);
json.toString();
then update in my text field with new json string in mysql
然后在我的文本字段中使用 mysql 中的新 json 字符串更新
I can't make it. I don't want use many class is there a simple way to do so with Hymanson?
我做不到。我不想使用很多课程,Hyman逊有没有一种简单的方法可以做到这一点?
回答by cmbaxter
Try casting your JsonNode
to an com.fasterxml.Hymanson.databind.node.ObjectNode
and then calling putset
(or replace
) on it.
尝试将您的转换JsonNode
为 ancom.fasterxml.Hymanson.databind.node.ObjectNode
然后在其上调用putset
(或replace
)。