java 在java中获取JSON字符串JsonNode中的所有键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48785312/
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
Get all the keys in a JSON string JsonNode in java
提问by Senchu Thomas
I have a json string which I need to validate and find any other keys other than in a list is there in the json string. The sample json string is
我有一个 json 字符串,我需要验证它,并在 json 字符串中找到除列表之外的任何其他键。示例 json 字符串是
{
"required" : true,
"requiredMsg" : "Title needed",
"choices" : [ "a", "b", "c", "d" ],
"choiceSettings" : {
"a" : {
"exc" : true
},
"b" : { },
"c" : { },
"d" : {
"textbox" : {
"required" : true
}
}
},
"Settings" : {
"type" : "none"
}
}
To allow only predifined keys is exsist in the json string I want to get all the keys in the json string. How can I obtain all the keys in the json string. I am using jsonNode. My code till now is
为了只允许预定义的键存在于 json 字符串中,我想获取 json 字符串中的所有键。如何获取 json 字符串中的所有键。我正在使用 jsonNode。到目前为止我的代码是
JsonNode rootNode = mapper.readTree(option);
JsonNode reqiredMessage = rootNode.path("reqiredMessage");
System.out.println("msg : "+ reqiredMessage.asText());
JsonNode drNode = rootNode.path("choices");
Iterator<JsonNode> itr = drNode.iterator();
System.out.println("\nchoices:");
while (itr.hasNext()) {
JsonNode temp = itr.next();
System.out.println(temp.asText());
}
How to get all the keys from the json string using JsonNode
如何使用从 json 字符串中获取所有键 JsonNode
采纳答案by pvpkiran
This should do it.
这应该做。
Map<String, Object> treeMap = mapper.readValue(json, Map.class);
List<String> keys = Lists.newArrayList();
List<String> result = findKeys(treeMap, keys);
System.out.println(result);
private List<String> findKeys(Map<String, Object> treeMap , List<String> keys) {
treeMap.forEach((key, value) -> {
if (value instanceof LinkedHashMap) {
Map<String, Object> map = (LinkedHashMap) value;
findKeys(map, keys);
}
keys.add(key);
});
return keys;
}
This will print out result as
这将打印出结果为
[required, requiredMsg, choices, exc, a, b, c, required, textbox, d, choiceSettings, type, Settings]
回答by Manos Nikolaidis
forEach
will iterate over children of a JsonNode
(converted to String
when printed) and fieldNames()
gets an Iterator<String>
over keys. Here are some examples for printing elements of the example JSON:
forEach
将迭代 a 的子项JsonNode
(String
在打印时转换为)并fieldNames()
获得一个Iterator<String>
over 键。以下是打印示例 JSON 元素的一些示例:
JsonNode rootNode = mapper.readTree(option);
System.out.println("\nchoices:");
rootNode.path("choices").forEach(System.out::println);
System.out.println("\nAllKeys:");
rootNode.fieldNames().forEachRemaining(System.out::println);
System.out.println("\nChoiceSettings:");
rootNode.path("choiceSettings").fieldNames().forEachRemaining(System.out::println);
You'll probably need fields()
at some point that returns an Iterator<Entry<String, JsonNode>>
so you can iterate over key, value pairs.
您可能fields()
在某个时候需要返回一个,Iterator<Entry<String, JsonNode>>
以便您可以迭代键、值对。
回答by Dave
The accepted answer works out great but issues a warning, "Type safety: The expression of type Map
needs unchecked conversion to conform to Map <String, Object>
"
接受的答案效果很好,但发出警告,“类型安全:类型的表达Map
需要未经检查的转换才能符合Map <String, Object>
”
This answerled me to change that line to the following to eliminate the warning:
这个答案使我将该行更改为以下内容以消除警告:
Map<String, Object> treeMap = mapper.readValue(json, new TypeReference<Map<String, Object>>() {});