作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 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/
如果我们的客户、供应商和分支机构都具有共同的属性(例如地址),那么哪个是更好的解决方案,为什么? 解决方案一: 客户表 供应商表 支表 具有 CustomerID、VendorID、BranchID
我很难使用 JSON.net 获取我的 JSON 对象的父键/属性/属性。也就是说,我想要作为字符串的最外层 属性名称,而事先/视觉上并不知道它是什么。我目前正在遍历一组 KeyValuePair 项
我是一名优秀的程序员,十分优秀!