JAVA入门到精通-fastJSON判断多层嵌套JSON数据中是否包含特定key

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

获取复杂json 某个指定key的值

添加依赖:
      <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>2.4.0</version>
        </dependency>
        
github: https://github.com/json-path/JsonPath        
    
代码demo:
JSONObject dataJson = new JSONObject();
dataJson.put("a", "123");
JSONObject aJson = new JSONObject();
aJson.put("b", "456123213123");
dataJson.put("c", aJson);
System.out.println(JsonPath.read(dataJson.toJSONString(), "@.c.b"));   

public static  void  main(String[] args){
        String jsonStr ="{\"properties\":{\"user_utt\":{\"fielddata\":\"111\",\"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\"}}}";
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        JSONObject dataJson = new JSONObject();
        dataJson.put("a", "123");
        JSONObject aJson = new JSONObject();
        aJson.put("b", "456123213123");
        dataJson.put("c", aJson);
        Object object = JsonPath.read(dataJson.toJSONString(), "*.b");
        System.out.println(dataJson.toJSONString());
        System.out.println(object);

        Object object2 = JsonPath.read(jsonStr, ".type");
        System.out.println(object2);
        System.out.println("key:" + getKeyFormJson(jsonObject,"analyzer"));
    }


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

未来AI编程

共鸣===鼓励 打赏您随意

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值