为什么JsonNode.get(“key”).asText()返回空字符串

i5desfxk  于 2023-04-13  发布在  其他
关注(0)|答案(1)|浏览(450)

我在保存到playersJson String时遇到了问题,数据来自节点“players”。节点是基于json dataJson创建的,使用byte[] playesFile创建的。我怀疑错误是由于这些字符串解析不正确造成的?
结果,StringplayersJson返回一个空String,我希望它包含json格式的关于“players”的信息,然后我将其放入Map〈String,Player〉
代码片段:

byte[] playersFile = storageService.downloadFile(FILENAME);
            String dataJson = new String(playersFile);

            try {
                JsonNode jsonNode = objectMapper.readTree(dataJson);
                String playersJson = jsonNode.get("players").asText();
                data = objectMapper.readValue(playersJson, new TypeReference<Map<String, Player>>(){});
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }

字符串dataJson:

{
"players":
 [
  {
    "key": "03a2452c-9d6b-47f5-9616-9a6833312762",
    "value": {
              "uuid": "2ae8d022-e0f4-4502-8e0e-1874997543e3",
              "name": "Artur",
              "elo": 2000,
              "gamesPlayed": 0,
              "email": "arti123i@mail.com"
              }
  },
  {
    "key": "8526db7c-6930-45bf-9ae1-fb93e97ff4ba",
    "value": {
             "uuid": "1bb43d73-3f94-40fc-a680-99f4a9304001",
             "name": "Kamil",
             "elo": 2000,
             "gamesPlayed": 0,
             "email": "kamil223@mail.com"
             }
 },
 {
    "key": "5a65ba8c-2180-464b-afe4-69c29b785282",
    "value": {
             "uuid": "b3dc8c98-5759-433e-88cd-0233946b9241",
             "name": "Marek",
             "elo": 3000,
             "gamesPlayed": 0,
             "email": "marek111@mail.com"
             }
 }
],
"games":
  [
   {
    "reportedTime": "2022-07-11T14:43:10.0354202+02:00",
    "reportedBy": {
      "uuid": "2ae8d022-e0f4-4502-8e0e-1874997543e3",
      "name": "Artur",
      "elo": 2016,
      "gamesPlayed": 1,
      "email": "arti123i@mail.com"
    },
    "result": [
      [
        {
          "player": {
            "uuid": "2ae8d022-e0f4-4502-8e0e-1874997543e3",
            "name": "Artur",
            "elo": 2016,
            "gamesPlayed": 1,
            "email": "arti123i@mail.com"
          },
          "eloBefore": 2000,
          "eloAfter": 2016
        },
        {
          "player": {
            "uuid": "1bb43d73-3f94-40fc-a680-99f4a9304001",
            "name": "Kamil",
            "elo": 2016,
            "gamesPlayed": 1,
            "email": "kamil223@mail.com"
          },
          "eloBefore": 2000,
          "eloAfter": 2016
        }
      ],
      [
        {
          "player": {
            "uuid": "b3dc8c98-5759-433e-88cd-0233946b9241",
            "name": "Marek",
            "elo": 2968,
            "gamesPlayed": 1,
            "email": "marek111@mail.com"
          },
          "eloBefore": 3000,
          "eloAfter": 2968
        }
      ]
    ]
   }
  ]
}

code image
code debugging

9rnv2umw

9rnv2umw1#

asText()方法为ArrayObject节点返回null。对于您的示例,“players”键的值是一个对象。因此它返回为EMPTY String。相反,您可以使用toString()。这将返回作为对象值的String的值。
参考:https://www.baeldung.com/java-jsonnode-astext-vs-tostring
Jackson的文档:https://javadoc.io/static/com.fasterxml.jackson.core/jackson-databind/2.15.0-rc2/com/fasterxml/jackson/databind/JsonNode.html#asText--

相关问题