java判断多层嵌套JSON数据中是否包含特定key

功能:用来判断复杂json数据中是否包含特定key。

方法: 

private Boolean isContainKeyForJson(JSONObject json,String target,Boolean result) {
        if(!result) {
            for(String key :json.keySet()) {
                if(target.equals(key)) {
                    return true;
                }
                Object temp = json.get(key);
                if (temp instanceof JSONObject) {
                    JSONObject new_name = (JSONObject) temp;
                    result = isContainKeyForJson(new_name,target, false);
                }
                if(result) {
                    return result ;
                }
            }
        }
        return result ;
    }

测试数据:

{"properties":{"user_utt":{"fielddata":"true","analyzer":"ngram_analyzer","term_vector":"yes","type":"text"},"user_utt_lemma_yomi":{"fielddata":"true","analyzer":"whitespace","term_vector":"yes","type":"text"},"ui_reserve2":{"index":"not_analyzed","type":"keyword","type1":"keyword"},"ui_reserve1":{"index":"not_analyzed","type":"keyword"},"add_utt":{"index":"not_analyzed","type":"keyword"},"topic":{"index":"not_analyzed","type":"keyword"},"current_status":{"index":"not_analyzed","type":"integer"},"original_user_utt":{"index":"not_analyzed","type":"keyword"},"user_utt_length":{"index":"not_analyzed","type":"long"},"user_utt_lemma":{"fielddata":"true","analyzer":"whitespace","term_vector":"yes","type":"text"},"subtopic":{"index":"not_analyzed","type":"keyword"}}}

测试方法:

@Test
	public void test() throws IOException {
		JSONObject test = JSONObject.parseObject(BusinessConstant.UI_MAPPING_JA);

		System.out.println(isContainKeyForJson(test,"type1",false));
	}
	
	private Boolean isContainKeyForJson(JSONObject json,String target,Boolean result) {
		if(!result) {
			for(String key :json.keySet()) {
				if(target.equals(key)) {
					return true;
				}
				Object temp = json.get(key);
				if (temp instanceof JSONObject) {
					JSONObject new_name = (JSONObject) temp;
					result = isContainKeyForJson(new_name,target, false);
				}
				if(result) {
					return result ;
				}
			}
		}
		return result ;
	}

 

JSON 判断一个 key 是否存在可以通过以下方式: 假设有以下 JSON 对象: ```json { "name": "John", "age": 30, "address": { "city": "New York", "country": "USA" }, "hobbies": [ "reading", "traveling" ] } ``` 可以使用以下 Java 代码判断 `name` 和 `phone` 两个属性是否存在: ```java import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper mapper = new ObjectMapper(); String jsonString = "{...}"; // JSON 字符串 JsonNode jsonNode = mapper.readTree(jsonString); // 判断 key 是否存在 boolean hasName = jsonNode.has("name"); boolean hasPhone = jsonNode.has("phone"); ``` 在进行 JSON Patch 操作时,可以使用 `has` 操作来判断一个 key 是否存在,例如: ```json [ { "op": "test", "path": "/address/city", "value": "New York" }, { "op": "add", "path": "/phone", "value": "1234567890", "condition": { "op": "has", "path": "/name" } } ] ``` 上面的 JSON Patch 操作,当 `/name` 存在时才能执行添加 `/phone` 的操作。在执行时,可以先使用 `has` 操作来判断 `/name` 是否存在,例如: ```java JsonNode patchNode = ...; // JSON Patch 操作 JsonNode rootNode = ...; // JSON 对象 for (JsonNode patchOpNode : patchNode) { String op = patchOpNode.get("op").asText(); String path = patchOpNode.get("path").asText(); String value = patchOpNode.get("value").asText(); if (op.equals("add")) { JsonNode conditionNode = patchOpNode.get("condition"); if (conditionNode != null) { String conditionOp = conditionNode.get("op").asText(); String conditionPath = conditionNode.get("path").asText(); if (conditionOp.equals("has") && !rootNode.has(conditionPath)) { // 如果 conditionPath 不存在,则不执行 add 操作 continue; } } // 执行 add 操作 // ... } else if (op.equals("...")) { // ... } // ... } ``` 在执行添加 `/phone` 的操作时,如果 `/name` 不存在,则不执行该操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值