如何使用杰克逊API重命名JsonNode中的字段 [英] how to rename a field in a JsonNode using jackson API
本文介绍了如何使用杰克逊API重命名JsonNode中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有此JSON的JsonNode:
I have a JsonNode with this JSON in it :
{"temperature":17,"long":200,"lat":100}
我想将JsonNode更改为这样
I want to change the JsonNode to look like this
{"MyNewFieldName":17,"long":200,"lat":100}
是否可以使用Jackson API?
Is it possible using Jackson API ?
推荐答案
您将无法重命名键值JSON对中的键.您需要做的是创建一个具有相同值但具有不同键的新键值对,并删除旧的键值对.
You won't be able to rename keys in key-value JSON pairs. What you will need to do is create a new key-value pair with the same value but with a different key and remove the old one.
JsonNode node = ...;
ObjectNode object = (ObjectNode) node;
object.set("MyNewFieldName", node.get("temperature"));
object.remove("temperature");
这篇关于如何使用杰克逊API重命名JsonNode中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文