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
How to ignore case when searching a JSON Object
提问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_ORDER
comparator), you can probably do the following:
鉴于可以使用 TreeMap(即通过String.CASE_INSENSITIVE_ORDER
比较器)实现不区分大小写,您可能可以执行以下操作:
Implement your own
MyJSONObject
extendingTreeMap
where its methods will be just calling static methods ofJSONObject
with the same signatures and all required interfaces as inJSONObject
. In default constructor writesuper(String.CASE_INSENSITIVE_ORDER)
Implement
ContainerFactory
interface wherecreateObjectContainer
will return new instance ofMyJSONObject
(andcreateArrayContainer
will just return newJSONArray
).To run it with new container
MyContainerFactory
:StringReader in = new StringReader(yourJSONString); JSONParser parser = new JSONParser(); parser.parse(in, yourContainerFactory)
实现你自己的
MyJSONObject
扩展TreeMap
,它的方法将只是调用JSONObject
具有相同签名和所有必需接口的静态方法JSONObject
。在默认构造函数中写入super(String.CASE_INSENSITIVE_ORDER)
实现
ContainerFactory
接口 wherecreateObjectContainer
将返回的新实例MyJSONObject
(并且createArrayContainer
只会返回 newJSONArray
)。要使用新容器运行它
MyContainerFactory
:StringReader in = new StringReader(yourJSONString); JSONParser parser = new JSONParser(); parser.parse(in, yourContainerFactory)