[英]Jackson JSON Mapping exception in java
我正在處理有時會更改的json數據。 有時,細分只是一個列表,有時,細分首先是一個對象,然后是一個列表。 我從網址中流式傳輸json數據,所以我幾天前將其保存到txt文件中,這就是為什么今天的日期不顯示的原因。 編輯:更新的json數據更改並添加了我擁有的所有代碼片段。 這是兩個不同的json字符串: 這里的段只是一個列表
{
"timetableresult": {
"ttitem": [
{
"segment": [
{
"segmentid": {
"mot": {
"@displaytype": "B",
"@type": "BLT",
"#text": "Buss"
},
"carrier": {
"name": "Vasttrafik",
"url": "http://www.vasttrafik.se/",
"id": 279,
"number": 100
}
},
"direction": "Goteborg Nils Ericsonterminal",
"departure": {
"location": {
"@id": 7400300,
"@x": 12.932004,
"@y": 57.720838,
"name": "Boras Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 20:10"
},
"arrival": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 21:10"
}
},
{
"segmentid": {
"mot": {
"@displaytype": "G",
"@type": "G",
"#text": "Gang"
},
"distance": 218
},
"departure": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 21:10"
},
"arrival": {
"location": {
"@id": 7400002,
"@x": 11.973479,
"@y": 57.708895,
"name": "Goteborg Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 21:13"
}
}
]
},
{
"segment": [
{
"segmentid": {
"mot": {
"@displaytype": "B",
"@type": "BLT",
"#text": "Buss"
},
"carrier": {
"name": "Vasttrafik",
"url": "http://www.vasttrafik.se/",
"id": 279,
"number": 100
}
},
"direction": "Goteborg Nils Ericsonterminal",
"departure": {
"location": {
"@id": 7400300,
"@x": 12.932004,
"@y": 57.720838,
"name": "Boras Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 20:40"
},
"arrival": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 21:40"
}
},
{
"segmentid": {
"mot": {
"@displaytype": "G",
"@type": "G",
"#text": "Gang"
},
"distance": 218
},
"departure": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 21:40"
},
"arrival": {
"location": {
"@id": 7400002,
"@x": 11.973479,
"@y": 57.708895,
"name": "Goteborg Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 21:43"
}
}
]
},
{
"segment": [
{
"segmentid": {
"mot": {
"@displaytype": "B",
"@type": "BXB",
"#text": "Expressbuss"
},
"carrier": {
"name": "Swebus",
"url": "http://www.swebus.se/",
"id": 690,
"number": 830
}
},
"direction": "Goteborg Nils Ericsonterminal",
"remarks": {
"remark": [
{
"@id": "A7",
"#text": "Djur ej tillatet"
},
{
"@id": "AA",
"#text": "Endast 2 klass"
}
]
},
"departure": {
"location": {
"@id": 7400300,
"@x": 12.932004,
"@y": 57.720838,
"name": "Boras Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 20:50"
},
"arrival": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 21:55"
}
},
{
"segmentid": {
"mot": {
"@displaytype": "G",
"@type": "G",
"#text": "Gang"
},
"distance": 218
},
"departure": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 21:55"
},
"arrival": {
"location": {
"@id": 7400002,
"@x": 11.973479,
"@y": 57.708895,
"name": "Goteborg Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 21:58"
}
}
]
},
{
"segment": [
{
"segmentid": {
"mot": {
"@displaytype": "B",
"@type": "BLT",
"#text": "Buss"
},
"carrier": {
"name": "Vasttrafik",
"url": "http://www.vasttrafik.se/",
"id": 279,
"number": 100
}
},
"direction": "Goteborg Nils Ericsonterminal",
"departure": {
"location": {
"@id": 7400300,
"@x": 12.932004,
"@y": 57.720838,
"name": "Boras Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 21:10"
},
"arrival": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 22:10"
}
},
{
"segmentid": {
"mot": {
"@displaytype": "G",
"@type": "G",
"#text": "Gang"
},
"distance": 218
},
"departure": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 22:10"
},
"arrival": {
"location": {
"@id": 7400002,
"@x": 11.973479,
"@y": 57.708895,
"name": "Goteborg Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 22:13"
}
}
]
},
{
"segment": [
{
"segmentid": {
"mot": {
"@displaytype": "B",
"@type": "BLT",
"#text": "Buss"
},
"carrier": {
"name": "Vasttrafik",
"url": "http://www.vasttrafik.se/",
"id": 279,
"number": 100
}
},
"direction": "Goteborg Nils Ericsonterminal",
"departure": {
"location": {
"@id": 7400300,
"@x": 12.932004,
"@y": 57.720838,
"name": "Boras Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 21:40"
},
"arrival": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 22:40"
}
},
{
"segmentid": {
"mot": {
"@displaytype": "G",
"@type": "G",
"#text": "Gang"
},
"distance": 218
},
"departure": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 22:40"
},
"arrival": {
"location": {
"@id": 7400002,
"@x": 11.973479,
"@y": 57.708895,
"name": "Goteborg Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 22:43"
}
}
]
},
{
"segment": [
{
"segmentid": {
"mot": {
"@displaytype": "B",
"@type": "BLT",
"#text": "Buss"
},
"carrier": {
"name": "Vasttrafik",
"url": "http://www.vasttrafik.se/",
"id": 279,
"number": 101
}
},
"direction": "Goteborg Nils Ericsonterminal",
"departure": {
"location": {
"@id": 7400300,
"@x": 12.932004,
"@y": 57.720838,
"name": "Boras Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 21:55"
},
"arrival": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 23:01"
}
},
{
"segmentid": {
"mot": {
"@displaytype": "G",
"@type": "G",
"#text": "Gang"
},
"distance": 218
},
"departure": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 23:01"
},
"arrival": {
"location": {
"@id": 7400002,
"@x": 11.973479,
"@y": 57.708895,
"name": "Goteborg Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 23:04"
}
}
]
},
{
"segment": [
{
"segmentid": {
"mot": {
"@displaytype": "B",
"@type": "BLT",
"#text": "Buss"
},
"carrier": {
"name": "Vasttrafik",
"url": "http://www.vasttrafik.se/",
"id": 279,
"number": 100
}
},
"direction": "Goteborg Nils Ericsonterminal",
"departure": {
"location": {
"@id": 7400300,
"@x": 12.932004,
"@y": 57.720838,
"name": "Boras Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 22:10"
},
"arrival": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 23:10"
}
},
{
"segmentid": {
"mot": {
"@displaytype": "G",
"@type": "G",
"#text": "Gang"
},
"distance": 218
},
"departure": {
"location": {
"@id": 7420483,
"@x": 11.971856,
"@y": 57.710275,
"name": "Goteborg Nils Ericsonterminal"
},
"datetime": "2012-11-26 23:10"
},
"arrival": {
"location": {
"@id": 7400002,
"@x": 11.973479,
"@y": 57.708895,
"name": "Goteborg Centralstation",
"stationinfo": {
"@exists": "true"
}
},
"datetime": "2012-11-26 23:13"
}
}
]
}
]
}
}
由於字符有限,繼續顯示的位置略向下。
只需使用Json節點即可:
public class Demo {
public static void main(String[] args) throws IOException {
String json = "{\n" +
" \"timetableresult\": {\n" +
" \"ttitem\": [\n" +
" {\n" +
" \"segmentId\": 1,\n" +
" \"value\": \"someVal\"\n" +
" },\n" +
" {\n" +
" \"segmentId\": 2,\n" +
" \"value\": \"someAnotherVal\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"}";
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readValue(json, JsonNode.class);
JsonNode jsonArray = node.get("timetableresult").get("ttitem");
System.out.println(jsonArray.get(1)); //will return null if it couldn't find
}
}
它將自動為您的json創建具有所有所需映射的樹結構。
傑克遜最好的行家依賴:
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
該文檔: http : //fasterxml.github.io/jackson-databind/javadoc/2.0.0/com/fasterxml/jackson/databind/JsonNode.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.