gpt4 book ai didi

java - 使用 Jackson 获取 JSON 值的最高父键

转载 作者:行者123 更新时间:2023-11-30 05:42:27 26 4
gpt4 key购买 nike

我有一个 json

{
"yes":
{
"en": "Yes",
"de": "Ja"
},
"no":
{
"en": "No",
"de": "Nein"
}
}

我想要一个使用 jackson 的 java 函数,它可以找到特定 json 值可能的最高键。

例如,如果我将值传递为 Nein -> 则位于顶层的 no Key 应该是输出。我怎样才能在java中完成这个?

最佳答案

那么,您可以尝试如下操作:

   public static void getRootNodeOfJSONObject() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = "{\"yes\":{\"en\": \"Yes\",\"de\": \"Ja\"},\"no\": {\"en\": \"No\",\"de\": \"Nein\"}}";
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode jsonNodeRoot = objectMapper.readTree(jsonString);

for (Iterator key = jsonNodeRoot.fields(); key.hasNext();) {
String text = key.next().toString();
if(text.contains("Nein"))
{
String rootElement = text.substring(0, text.indexOf("="));
System.out.println("Root element: " + rootElement);
}
}
}

public static void main(String[] args) throws IOException {
getRootNodeOfJSONObject();
}

关于java - 使用 Jackson 获取 JSON 值的最高父键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55411503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com