我正在处理有时会发生变化的json数据。有时Segment只是一个列表,有时segment首先是一个对象,然后是一个列表。我从一个网址流式传输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"
}
}
]
}
]
}
}
由于角色有限,继续显示得更远......
答案 0 :(得分:0)
只需使用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>