Java 带Jackson的Travesing JSON始终为一个特定节点返回null
我正在拉一个JSON格式的RESTful API,并遍历节点来检索我要查找的信息。但是有一个特定节点总是以null开头,即使JSON文档看起来很好,并且该特定节点不包含任何null值 我是使用Jackson解析JSON文档的新手,我不知道为什么特定节点总是在节点值前面返回null 我遇到问题的节点是StationName,它返回例如nullHägerstensåsen:。我希望检索的结果是Hägerstensåsen:没有空值 RESTful服务中的原始JSON文档:Java 带Jackson的Travesing JSON始终为一个特定节点返回null,java,json,jakarta-ee,jackson,Java,Json,Jakarta Ee,Jackson,我正在拉一个JSON格式的RESTful API,并遍历节点来检索我要查找的信息。但是有一个特定节点总是以null开头,即使JSON文档看起来很好,并且该特定节点不包含任何null值 我是使用Jackson解析JSON文档的新手,我不知道为什么特定节点总是在节点值前面返回null 我遇到问题的节点是StationName,它返回例如nullHägerstensåsen:。我希望检索的结果是Hägerstensåsen:没有空值 RESTful服务中的原始JSON文档: { "Departur
{
"Departure": {
"xmlnsxsi": "http://www.w3.org/2001/XMLSchema-instance",
"xmlnsxsd": "http://www.w3.org/2001/XMLSchema",
"xmlns": "http://www1.sl.se/realtidws/",
"LatestUpdate": "2014-01-21T11:34:36.7189974+01:00",
"ExecutionTime": "00:00:00.0937512",
"Buses": {},
"Metros": {
"Metro": [
{
"SiteId": "9262",
"TransportMode": "METRO",
"StationName": "Hägerstensåsen",
"GroupOfLine": "Tunnelbanans röda linje",
"DisplayRow1": "14 Fruängen 2 min",
"DisplayRow2": "14 Fruängen 12 min, 14 Fruängen 23 min"
},
{
"SiteId": "9262",
"TransportMode": "METRO",
"StationName": "Hägerstensåsen",
"GroupOfLine": "Tunnelbanans röda linje",
"DisplayRow1": "14 Mörby centrum 2 min.",
"DisplayRow2": "14 Mörby centrum 12 min. 14 Mörby centrum 22 min."
}
]
},
"Trains": {},
"Trams": {},
"TrainError": {
"HasError": "true",
"FaultCode": "Client",
"ErrorLevel": "Error",
"ErrorCode": "1000",
"ErrorSource": "/realtidws/RealTimeService.asmx/GetDepartures",
"ErrorMessage": "Connection string is missing"
},
"TramError": {
"HasError": "true",
"FaultCode": "Client",
"ErrorLevel": "Error",
"ErrorCode": "1000",
"ErrorSource": "/realtidws/RealTimeService.asmx/GetDepartures",
"ErrorMessage": "Connection string is missing"
}
}
}
我的节点解析代码:
...
result = null;
JsonNode node = mapper.readTree(slRealTimeClient.getJson(depatureSite));
node = node.path("Departure").path("Metros").path("Metro");
JsfUtil.debug("Node size: " + node.size());
depatureSiteResponse = node.get(0).path("SiteId").getTextValue();
for (JsonNode element : node) {
if (!element.isNull()) {
result += element.get("StationName").getTextValue();
result += ":" + System.getProperty("line.separator");
result += element.get("DisplayRow1").getTextValue();
result += System.getProperty("line.separator");
result += element.get("DisplayRow2").getTextValue();
result += System.getProperty("line.separator");
}
}
经过一些调试,我想出了一个解决方案。在开始获取文本值之前,我刚刚分配了一个新的字符串对象。在我给它赋值null之前,先清除String对象。复制文本值时为什么保留null作为文本值我不知道 这是解决办法
result = new String();
JsonNode node = mapper.readTree(slRealTimeClient.getJson(depatureSite));
node = node.path("Departure").path("Metros").path("Metro");
JsfUtil.debug("Node size: " + node.size());
depatureSiteResponse = node.get(0).path("SiteId").getTextValue();
for (JsonNode element : node) {
if (!element.isNull()) {
result += element.get("StationName").getTextValue();
result += ":" + System.getProperty("line.separator");
result += element.get("DisplayRow1").getTextValue();
result += System.getProperty("line.separator");
result += element.get("DisplayRow2").getTextValue();
result += System.getProperty("line.separator");
}
}
尝试打印原始REST响应,或在调试器中检查字符串。我有一种感觉,那里可能有一个额外的不可打印字符,这会把事情搞得一团糟。我猜这与JSON无关,而是实际的字符数据。您可能在不指定UTF8代码页的情况下转换为JSON的8字节表示形式,或者在StationName字符串中实际嵌入了一个不可见字符。提示:使用toString转储Metro数组的元素。@CodeChimp JSON文档是原始交易@热舔是的,这是我的一个想法,一个看不见的字符。正如您所说,可能只是获取整个数组并按原样打印它。实际上,如果我注释掉StationName并检索DisplayRow1节点,同样的问题也会发生。访问数组元素的正确方法是什么?您可以简单地分配而不是新字符串。分配空字符串与新字符串相同。垃圾收集器最终会将其清理干净。便宜多了。它是在加载类并将其插入时创建的,因此不需要对其进行GCD。鉴于您在循环上重复添加字符串,Java编译器可能无法优化字符串连接。我觉得在这里有合法的用途。