0
点赞
收藏
分享

微信扫一扫

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

丹柯yx 2022-05-27 阅读 103

判断多层嵌套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 条评论