简体   繁体   English

当使用Jackson只有一个值时,如何读取根节点?

[英]How to read the root nodes when they only have a value using Jackson?

I read in a valid JSON file, which has the format shown below (I have no control in that) with only values for the root nodes, using: 我读了一个有效的JSON文件,它有下面显示的格式(我没有控制权),只有根节点的值,使用:

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
JsonNode rootNode = jsonMapper.readTree(belowString);
  1. How do I get the root nodes names (First and second below), which I do not know? 如何获取根节点名称(下面的第一个和第二个),我不知道?
  2. Subsequently, I also need to read the attending value 随后,我还需要阅读参加价值
{
"First": [{

        "name": "Bill",
        "groupName": "team1",
        "groupType": "golf",
        "info": [{
            "name": "George",
            "groupName": "Caddy"
        }],
        "attending": false
    },
    {
        "name": "Fred",
        "groupName": "team2",
        "groupType": "golf",
        "info": [{
            "name": "Todd",
            "groupName": "caddy"
        }],
        "attending": false
    },
    {
        "name": "Mike",
        "groupName": "team3",
        "groupType": "golf",
        "info": [{
            "name": "Peter",
            "groupName": "caddy"
        }],
        "attending": false
    }
],
"Second": [{

    "name": "Alan",
    "groupName": "team4",
    "groupType": "golf",
    "info": [{
        "name": "Tony",
        "groupName": "caddy"
    }],
    "attending": false
}]
}

The accepted answer solved #1. 接受的答案解决了#1。 This is the resolution I used for #2 to access the nested nodes: 这是我用于#2访问嵌套节点的分辨率

while (iter.hasNext()) {
   Map.Entry<String, JsonNode> entry = iter.next();

   System.out.println("key: " + entry.getKey());
   System.out.println("value: " + entry.getValue());

   if (entry.getValue().isArray()) {
       JsonNode attending = entry.getValue().get(1).get("attending");
       System.out.println("attending = " + attending.toString());
   }
}

You are almost there! 你快到了! Find below a quick snippet. 在下面找到一个快速代码段。 Hope this will help. 希望这会有所帮助。

JsonNode node = mapper.readTree( "{\"First\":[{\"name\":\"Bill\",\"groupName\":\"team1\",\"groupType\":\"golf\",\"info\":[{\"name\":\"George\",\"groupName\":\"Caddy\"}],\"attending\":false},{\"name\":\"Fred\",\"groupName\":\"team2\",\"groupType\":\"golf\",\"info\":[{\"name\":\"Todd\",\"groupName\":\"caddy\"}],\"attending\":false},{\"name\":\"Mike\",\"groupName\":\"team3\",\"groupType\":\"golf\",\"info\":[{\"name\":\"Peter\",\"groupName\":\"caddy\"}],\"attending\":false}],\"Second\":[{\"name\":\"Alan\",\"groupName\":\"team4\",\"groupType\":\"golf\",\"info\":[{\"name\":\"Tony\",\"groupName\":\"caddy\"}],\"attending\":false}]}".getBytes() );
node.fields().forEachRemaining( entry -> System.out.println( "Key "+ entry.getKey() + " Value "+ entry.getValue()) );

Output: 输出:

Key First Value [{"name":"Bill","groupName":"team1","groupType":"golf","info":[{"name":"George","groupName":"Caddy"}],"attending":false},{"name":"Fred","groupName":"team2","groupType":"golf","info":[{"name":"Todd","groupName":"caddy"}],"attending":false},{"name":"Mike","groupName":"team3","groupType":"golf","info":[{"name":"Peter","groupName":"caddy"}],"attending":false}]
Key Second Value [{"name":"Alan","groupName":"team4","groupType":"golf","info":[{"name":"Tony","groupName":"caddy"}],"attending":false}]

To get the field names: 要获取字段名称:

Iterator<String> iter = rootNode.fieldNames();
while (iter.hasNext()) {
    System.out.println("field: " + iter.next());
}

To get the names and the values: 获取名称和值:

Iterator<Entry<String, JsonNode>> iter = rootNode.fields();
while (iter.hasNext()) {
    Entry<String, JsonNode> entry = iter.next();
    System.out.println("key: " + entry.getKey());
    System.out.println("value: " + entry.getValue());
}

You could also deserialize this Json as a Map with readValue(..) if readTree(..) is not a requirement, like: 如果readTree(..)不是readTree(..)你也可以将这个Json反序列化为带有readValue(..)Map ,如:

@Test
public void test() throws Exception {
    ObjectMapper jsonMapper = new ObjectMapper();
    // JSON contains this kind of a structure
    Map<String, List<Map<String, Object>>> map = jsonMapper
                // test.json should be in the same package as test and contain 
                // your Json
                .readValue(getClass().getResourceAsStream("test.json"), Map.class);
    // Result of this loggion below
    map.entrySet().forEach(entry -> {
        log.info("key: {}", entry.getKey());
        entry.getValue().forEach(person -> {
            log.info(" {} attending: {}", person.get("name"), person.get("attending"));
        });
    });
}

Log should be like: 日志应该像:

key: First 关键:首先
Bill attending: false 比尔参加:假
Fred attending: false 弗雷德参加:假
Mike attending: false 迈克参加:假
key: Second 关键:第二
Alan attending: false 艾伦出席:假

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM