REST Assured 57 - Editing Existing JSON Object On The Fly Using JsonNode – Jackson

REST Assured 系列汇总 之 REST Assured 57 - Editing Existing JSON Object On The Fly Using JsonNode – Jackson

介绍

很多时候我们不想创建 POJO 类,根据需求我们需要编辑一个存在的 JSON。这种情况下,Jackson API 提供的 JsonNode 就可以帮忙了。

前提条件

因为需要用到 Jackson API,所以需要添加 Jackson Databind 依赖包。

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.3</version>
</dependency>

在开始本篇内容前,请先阅读下面文章,有助于理解:
How To Create A JSON Object Using Jackson API – ObjectMapper – CreateObjectNode()
How To Create JSON Array Using Jackson API – ObjectMapper – CreateArrayNode()

Editing JSON using JsonNode

Adding a new key with a primitive value to existing JSON

已经存在的 JSON String:

{
    
    
  "firstname": "Kevin",
  "lastname": "Zhang"
}

加一个新的字段 “role”, 值是 “admin”
我们将上面的 JSON object 反序列化 deserialize 成 ObjectNode。请注意 ObjectNode 类的父类是 JsonNode 类。

import org.junit.Test;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
 
public class EditJsonObjectOnFly {
    
    
 
	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
    
    
		String jsonObject = "{\r\n" + 
				"  \"firstname\": \"Kevin\",\r\n" + 
				"  \"lastname\": \"Zhang\"\r\n" + 
				"}";
 
		// Creating an instance of ObjectMapper class
		ObjectMapper objectMapper = new ObjectMapper();
		// Get ObjectNode representation of json as json is an Object
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		objectNode.put("role", "admin");
		System.out.println(objectNode.toPrettyString());
 
	}
 
}

输出:

{
    
    
  "firstname" : "Kevin",
  "lastname" : "Zhang",
  "role" : "admin"
}

Adding a new key with a JSON object value to an existing JSON
增加一个新 key,其值是一个 JSON object。例如:增加一个新的字段 “bookingDetails”,其值如下。本质上,我们要添加一个 ObjectNode,相关的链接就在前提条件章节中。

"bookingDetails" : {
    
    
    "firstname" : "Jim",
    "lastname" : "Brown"
}

代码:

import org.junit.Test;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
 
public class EditJsonObjectOnFly2 {
    
    
 
	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
    
    
		String jsonObject = "{\r\n" + 
				"  \"firstname\": \"Kevin\",\r\n" + 
				"  \"lastname\": \"Zhang\"\r\n" + 
				"}";
 
		// Creating an instance of ObjectMapper class
		ObjectMapper objectMapper = new ObjectMapper();
		// Get ObjectNode representation of json as json is an Object
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		
		ObjectNode bookingDetails = objectMapper.createObjectNode();
		bookingDetails.put("firstname", "Jim");
		bookingDetails.put("lastname", "Brown");
 
		objectNode.set("bookingDetails", bookingDetails);
		System.out.println(objectNode.toPrettyString());
 
	}
 
}

输出:

{
    
    
  "firstname" : "Kevin",
  "lastname" : "Zhang",
  "bookingDetails" : {
    
    
    "firstname" : "Jim",
    "lastname" : "Brown"
  }
}

Adding a new key inside a nested JSON Object
在上面输出上,建设我们需要增加另外一个键值对在 “bookingDetails” 字段里面,例如最终输出如下:

"bookingDetails" : {
    
    
    "firstname" : "Jim",
    "lastname" : "Brown",
    "gender" : "male"
  }

代码:

import org.junit.Test;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
 
public class EditJsonObjectOnFly3 {
    
    
 
	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
    
    
		String jsonObject = "{\r\n" + 
				"  \"firstname\" : \"Kevin\",\r\n" + 
				"  \"lastname\" : \"Zhang\",\r\n" + 
				"  \"bookingDetails\" : {\r\n" + 
				"    \"firstname\" : \"Jim\",\r\n" + 
				"    \"lastname\" : \"Brown\"\r\n" + 
				"  }\r\n" + 
				"}";
 
		// Creating an instance of ObjectMapper class
		ObjectMapper objectMapper = new ObjectMapper();
		// Get ObjectNode representation of json as json is an Object
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		
		objectNode.with("bookingDetails").put("gender", "male");
		System.out.println(objectNode.toPrettyString());
 
	}
 
}

Update value of existing keys
更新外层和嵌套的 firstname 字段的值

import org.junit.Test;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
 
public class EditJsonObjectOnFly4 {
    
    
 
	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
    
    
		String jsonObject = "{\r\n" + 
				"  \"firstname\" : \"Kevin\",\r\n" + 
				"  \"lastname\" : \"Zhang\",\r\n" + 
				"  \"bookingDetails\" : {\r\n" + 
				"    \"firstname\" : \"Jim\",\r\n" + 
				"    \"lastname\" : \"Brown\"\r\n" + 
				"  }\r\n" + 
				"}";
 
		// Creating an instance of ObjectMapper class
		ObjectMapper objectMapper = new ObjectMapper();
		// Get ObjectNode representation of json as json is an Object
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
		
		objectNode.put("firstname", "Peter");
		objectNode.with("bookingDetails").put("firstname", "Rahul");
		System.out.println(objectNode.toPrettyString());
	}
 
}

输出:

{
    
    
  "firstname" : "Peter",
  "lastname" : "Zhang",
  "bookingDetails" : {
    
    
    "firstname" : "Rahul",
    "lastname" : "Brown"
  }
}

Remove or delete existing key
删除外层和嵌套的 firstname 字段的值

import org.junit.Test;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
 
public class EditJsonObjectOnFly5 {
    
    
 
	@Test
	public void quickEditToJsonObject() throws JsonMappingException, JsonProcessingException {
    
    
		String jsonObject = "{\r\n" + "  \"firstname\" : \"Kevin\",\r\n" + "  \"lastname\" : \"Zhang\",\r\n"
				+ "  \"bookingDetails\" : {\r\n" + "    \"firstname\" : \"Jim\",\r\n"
				+ "    \"lastname\" : \"Brown\"\r\n" + "  }\r\n" + "}";
 
		// Creating an instance of ObjectMapper class
		ObjectMapper objectMapper = new ObjectMapper();
		// Get ObjectNode representation of json as json is an Object
		ObjectNode objectNode = objectMapper.readValue(jsonObject, ObjectNode.class);
 
		objectNode.remove("firstname");
		objectNode.with("bookingDetails").remove("firstname");
 
		System.out.println(objectNode.toPrettyString());
 
	}
 
}

输出:

{
    
    
  "lastname" : "Kevin",
  "bookingDetails" : {
    
    
    "lastname" : "Brown"
  }
}

猜你喜欢

转载自blog.csdn.net/wumingxiaoyao/article/details/120587291
今日推荐