Java 搜索 JSON 对象时如何忽略大小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18066844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:09:59  来源:igfitidea点击:

How to ignore case when searching a JSON Object

javajsonjson-simple

提问by Chaos

My sample JSON input is as follows:

我的示例 JSON 输入如下:

"JobName":"Test Job 1",
"events":[
    {   "features":[],
        "InputHiveTable":"uilog_uiclientlogdata",
        "eventColumn":"command",
        "name":"edu.apollogrp.classroom.discussion.client.events.CreateDiscussionEvent"
    },

Consider the field "InputHiveTable", it could be in all uppercase INPUTHIVETABLE, all lowercase inputhivetable, or a mixture of both as it is now.

考虑字段"InputHiveTable",它可以全部为大写INPUTHIVETABLE,全部为小写inputhivetable,或者像现在这样两者的混合。

Currently, I'm reading the field as follows (in Java):

目前,我正在阅读该领域如下(在 Java 中):

JSONObject jsonObject = (JSONObject) obj;
JSONArray events = (JSONArray) jsonObject.get("events");
String InputHiveTable = (String)event.get("InputHiveTable");

So my question is how do I search for the field "InputHiveTable"while ignoring the case. I'm using JSON Simple libraries.

所以我的问题是如何"InputHiveTable"在忽略大小写的情况下搜索该字段。我正在使用 JSON 简单库。

采纳答案by Chris Gerken

If you have to perform this case-insensitive lookup many times, I'd just write a method to do that lookup:

如果您必须多次执行这种不区分大小写的查找,我只需编写一个方法来执行该查找:

public Object getIgnoreCase(JSONObject jobj, String key) {

    Iterator<String> iter = jobj.keySet().iterator();
    while (iter.hasNext()) {
        String key1 = iter.next();
        if (key1.equalsIgnoreCase(key)) {
            return jobj.get(key1);
        }
    }

    return null;

}

回答by Zoltorn

You could read the JSONObject into a java string, and call String.toLowerCase on it and store it back into a JSONObject. This will turn the entire case of the string to lower case, so you will have to account for that elsewhere in your logic. After that, you then would just have to do a get call on "inputhivetable".

您可以将 JSONObject 读入一个 java 字符串,并对其调用 String.toLowerCase 并将其存储回 JSONObject。这会将字符串的整个大小写转换为小写,因此您必须在逻辑中的其他地方考虑到这一点。之后,您只需对“inputhivetable”进行 get 调用。

By no means is it a pretty solution, but it is a potential work around if there is absolutely no other way for you to handle what you're returning as your JSON input.

这绝不是一个很好的解决方案,但如果您绝对没有其他方法来处理您作为 JSON 输入返回的内容,那么它是一个潜在的解决方案。

回答by Alex P

Given that case-insensitivity can be achieved with TreeMap (i.e. via String.CASE_INSENSITIVE_ORDERcomparator), you can probably do the following:

鉴于可以使用 TreeMap(即通过String.CASE_INSENSITIVE_ORDER比较器)实现不区分大小写,您可能可以执行以下操作:

  1. Implement your own MyJSONObjectextending TreeMapwhere its methods will be just calling static methods of JSONObjectwith the same signatures and all required interfaces as in JSONObject. In default constructor write super(String.CASE_INSENSITIVE_ORDER)

  2. Implement ContainerFactoryinterface where createObjectContainerwill return new instance of MyJSONObject(and createArrayContainerwill just return new JSONArray).

  3. To run it with new container MyContainerFactory:

     StringReader in = new StringReader(yourJSONString);                    
     JSONParser parser = new JSONParser();      
     parser.parse(in, yourContainerFactory)
    
  1. 实现你自己的MyJSONObject扩展TreeMap,它的方法将只是调用JSONObject具有相同签名和所有必需接口的静态方法JSONObject。在默认构造函数中写入super(String.CASE_INSENSITIVE_ORDER)

  2. 实现ContainerFactory接口 wherecreateObjectContainer将返回的新实例MyJSONObject(并且createArrayContainer只会返回 new JSONArray)。

  3. 要使用新容器运行它MyContainerFactory

     StringReader in = new StringReader(yourJSONString);                    
     JSONParser parser = new JSONParser();      
     parser.parse(in, yourContainerFactory)