简体   繁体   English

如何使用jackson API重命名JsonNode中的字段

[英]how to rename a field in a JsonNode using jackson API

I have a JsonNode with this JSON in it : 我有一个带有这个JSON的JsonNode:

{"temperature":17,"long":200,"lat":100}

I want to change the JsonNode to look like this 我想将JsonNode更改为这样

{"MyNewFieldName":17,"long":200,"lat":100}

Is it possible using Jackson API ? 是否可以使用Jackson API?

You won't be able to rename keys in key-value JSON pairs. 您将无法重命名键值JSON对中的键。 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM