使用Jackson读取根节点只有值时如何读取?

时间:2019-01-15 18:40:05

标签: java json jackson

我使用以下方法读取了一个有效的JSON文件,该文件的格式如下所示(我无法控制),其中只有根节点的值:

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
JsonNode rootNode = jsonMapper.readTree(belowString);
  1. 如何获取我不知道的根节点名称(下面的第一和第二个)?
  2. 随后,我还需要读取出席值
 
{
"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
}]
}

接受的答案解决了#1。 这是我用于#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());
   }
}

3 个答案:

答案 0 :(得分:1)

您快到了!在下面找到一个快速摘要。希望这会有所帮助。

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()) );

输出:

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}]

答案 1 :(得分:1)

要获取字段名称:

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

要获取名称和值:

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());
}

答案 2 :(得分:1)

如果不需要Map,也可以使用readValue(..)将此Json反序列化为readTree(..),例如:

@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"));
        });
    });
}

日志应类似于:

  

键:第一
   出席账单:假
   弗雷德参加:假
   迈克参加:假
  关键:第二名
   艾伦参加:错误