JAVA入门到精通-fastJSON判断多层嵌套JSON数据中是否包含特定key
【摘要】
判断多层嵌套JSON数据中是否包含特定key
获取复杂json 某个指定key的值
添加依赖: <dependency> &nbs...
判断多层嵌套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"));
}
文章来源: blog.csdn.net,作者:隔壁老瓦,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/wxb880114/article/details/106015754
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)