如何检查JSON对象中是否存在具有特定值的键? [英] How to check if key with specific value exists in JSON object?

查看:119
本文介绍了如何检查JSON对象中是否存在具有特定值的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的JSON对象:

I have a JSON object like this:

{
    people: [
        {
            id: "id1",
            name: "name1",
            description: "Desc"
        },
        {
            id: "id2",
            name: "name2",
            description: "Desc2"
        },
        {
            id: "id3",
            name: "name3",
            description: "Desc3"
        }
    ]
}

我想检查是否存在具有"id2"值的id,java中是否有任何utils可以让我做到这一点,而无需遍历所有对象并将String与target进行比较?

I want to check if an id with value of "id2" exists, is there any utils in java that allow me to do this without iterating through all objects and comparing String with target?

P.S.我不想使用JSONObject.has(key)知道'id'字段是否存在,这不是我要的.

P.S. I don't want to know if 'id' field exists using JSONObject.has(key), this is not what I'm asking.

推荐答案

我个人使用net.sf.json库.使用该库, JSONObject 的方法包含(字符串键),它根据键是否存在返回一个布尔值.如果您希望进行如此大量的搜索,我建议您研究一下该库或Google的gson库,以使搜索起来更加轻松.

I personally use the net.sf.json library. With that library the JSONObject has a method contains(String key) which returns a Boolean based on if the key exists or not. If you are looking to search like this a lot I would suggest looking into that library or Google's gson library to make it easier on you.

这篇关于如何检查JSON对象中是否存在具有特定值的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆