栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

fastjson 修改多层嵌套的Json数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

fastjson 修改多层嵌套的Json数据

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录
  • 一、需求
  • 二、期望效果
  • 三、实现
  • 四、总结


一、需求

操作多层嵌套的Json数据,修改key值或删除key,然后更新原始Json数据

二、期望效果

删除原始Json中query-->bool-->should-->match_phrase-->recallChar2-->zero_terms_query字段(key),然后更新原始Json。

Json数据格式如下:
原始Json

{
	"query": {
		"bool": {
			"filter": [
				{
					"term": {
						"label.keyword": {
							"boost": 1.0,
							"value": "字符串"
						}
					}
				}
			],
			"adjust_pure_negative": true,
			"should": [
				{
					"match": {
						"recallChar2": {
							"auto_generate_synonyms_phrase_query": true,
							"query": "测试",
							"zero_terms_query": "NONE",
							"fuzzy_transpositions": true,
							"boost": 1.0,
							"prefix_length": 0,
							"operator": "OR",
							"lenient": false,
							"max_expansions": 50
						}
					}
				},
				{
					"match_phrase": {
						"recallChar2": {
							"query": "测试",
							"zero_terms_query": "NONE",
							"boost": 1.0,
							"slop": 0
						}
					}
				}
			],
			"boost": 1.0
		}
	}
}

新Json

{
	"query": {
		"bool": {
			"filter": [
				{
					"term": {
						"label.keyword": {
							"boost": 1.0,
							"value": "字符串"
						}
					}
				}
			],
			"adjust_pure_negative": true,
			"should": [
				{
					"match": {
						"recallChar2": {
							"auto_generate_synonyms_phrase_query": true,
							"query": "测试",
							"zero_terms_query": "NONE",
							"fuzzy_transpositions": true,
							"boost": 1.0,
							"prefix_length": 0,
							"operator": "OR",
							"lenient": false,
							"max_expansions": 50
						}
					}
				},
				{
					"match_phrase": {
						"recallChar2": {
							"query": "测试",
							"boost": 1.0,
							"slop": 0
						}
					}
				}
			],
			"boost": 1.0
		}
	}
}
三、实现
//入参esSearchRequestParam:原始Json	返参:新Json
private JSONObject updateEsRequestParam(JSONObject esSearchRequestParam) {
        Object bool = JSONObject.parseObject(esSearchRequestParam.get("query").toString()).get("bool");
        Object should = JSONObject.parseObject(bool.toString()).get("should");
        JSONArray shouldArray = JSONObject.parseArray(should.toString());
        for (int i = 0; i < shouldArray.size(); i++) {
            JSONObject jsonObject = shouldArray.getJSONObject(i);
            if (jsonObject.containsKey("match_phrase")){
                Object matchPhrase = jsonObject.get("match_phrase");
                Object recallChar2 = JSONObject.parseObject(matchPhrase.toString()).get("recallChar2");
                JSONObject recallChar2New = JSONObject.parseObject(recallChar2.toString());
//                    jsonObject1.fluentPut("zero_terms_query","测试");
                recallChar2New.remove("zero_terms_query");
                Object matchPhraseNew = JSONObject.parseObject(matchPhrase.toString()).fluentPut("recallChar2", recallChar2New);
                jsonObject.fluentPut("match_phrase", matchPhraseNew);
            }
        }
        JSONObject boolNew = JSONObject.parseObject(bool.toString()).fluentPut("should", shouldArray);
        JSONObject queryNew = JSONObject.parseObject(esSearchRequestParam.get("query").toString()).fluentPut("bool", boolNew);
        return esSearchRequestParam.fluentPut("query", queryNew);
    }
四、总结

遇到这类问题先分析Json的数据格式

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/985680.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号