I am pulling a RESTful API in JSON format and traverse through the nodes retrieve the info that I am looking for. But there is one specific node that always start with "null" even if the JSON document looks fine and that specific node doesn't contain any null value.
I am novice on using Jackson for parsing JSON document and I can't find out why the specific node always return "null" in front of the node value?
The node I have problem with is the StationName
, which return for example nullHägerstensåsen:
. The result that I hope to retrieve is Hägerstensåsen:
without the "null".
The raw JSON document from the RESTful service:
{
"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"
}
}
}
My code for parsing the nodes:
...
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");
}
}