您现在的位置是:首页 > 博文答疑 > Java JsonNode 判断元素是否存在博文答疑

Java JsonNode 判断元素是否存在

Leo2020-07-21【5】

简介Java JsonNode 判断元素是否存在

今天遇到个需求, API  发送JSON 字符串到 API Server ,需要考虑某些元素(节点)是否存在。如果不存在需要新增一个节点。如果接受到 null, 测无需新增节点。

以下是基于SpringBoot的测试和总结。希望对大家有点帮助。

 

直接上代码如下:

package com.test.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;


@RestController
@RequestMapping("/base")
public class BaseController {

	@RequestMapping("/jsonNode")
	public String getPerson(@RequestBody String input){
		System.out.println("input :   " + input);
		ObjectMapper objectMapper = new ObjectMapper();
		JsonNode jsonNode = null;
		try {
			jsonNode = objectMapper.readTree(input);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String nameAsText = jsonNode.get("name").asText();
		String nameToString = jsonNode.get("name").toString();
		System.out.println("nameAsText         :  " + nameAsText);
		System.out.println("nameToString       :  " + nameToString);

		System.out.println("name for findValue :  " + jsonNode.findValue("name"));
		System.out.println("name2 for findValue:  " + jsonNode.findValue("name2"));
		System.out.println("name3 for findValue:  " + jsonNode.findValue("name3"));

		System.out.println("found node name    :  " + jsonNode.has("name"));
		System.out.println("found node name2   :  " + jsonNode.has("name2"));
		System.out.println("found node name3   :  " + jsonNode.has("name3"));

		ObjectNode newNode = (ObjectNode) jsonNode;
		if (jsonNode.has("name2")){
			newNode.put("name2-flag", "Y");
		}
		if (jsonNode.has("name3")){
			newNode.put("name3-flag", "Y");
		}
		String jsonStr = "";
		try {
			jsonStr = objectMapper.writeValueAsString(jsonNode);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		System.out.println("output :   " + jsonStr);
		return jsonStr;
	}

}

 

运行  http://localhost:8080/base/jsonNode 

Request Body 输入: 

{
"name": "test name",
"name2": ""
}

 

System Out 输出结果:

=======结果1================================================================
input :   {
"name": "test name",
"name2": null
}
nameAsText         :  test name
nameToString       :  "test name"
name for findValue :  "test name"
name2 for findValue:  null
name3 for findValue:  null
found node name    :  true
found node name2   :  true
found node name3   :  false
output :   {"name":"test name","name2":null,"name2-flag":"Y"}

=======结果2================================================================
input :   {
"name": "test name",
"name2": ""
}
nameAsText         :  test name
nameToString       :  "test name"
name for findValue :  "test name"
name2 for findValue:  ""
name3 for findValue:  null
found node name    :  true
found node name2   :  true
found node name3   :  false
output :   {"name":"test name","name2":"","name2-flag":"Y"}

 

api 返回结果:

{"name":"test name","name2":"","name2-flag":"Y"}