Len's Study-Log

集中一点,登峰造极!

0%

Jackson文档翻译-Streaming Model examples

原文


本 Jackson 教程向你展示如何使用 JsonGenerator 把 JSON 字符串和 JSON 数组写入到文件中,以及,使用 JsonParser 来解析它们。

Jackson Streaming APIs

  • JsonGenerator – Write JSON
  • JsonParser – Parse JSON

注意

Jackson 流媒体模型是在 data-binding 和 Tree Model 这两个底层处理模型之上建立的。它在 JSON 解析和生成上拥有最好的性能和控制。

1. JSON 数据写入 - JsonGenerator

1.1 把 JSON 写入到文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// JacksonExample1.java

package com.mkyong;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

public class JacksonExample1 {

public static void main(String[] args) {

ObjectMapper mapper = new ObjectMapper();

try (JsonGenerator jGenerator =
mapper.getFactory().createGenerator(
new File("c:\\projects\\user.json")
, JsonEncoding.UTF8)) {

jGenerator.writeStartObject(); // {

jGenerator.writeStringField("name", "mkyong"); // "name" : "mkyong"
jGenerator.writeNumberField("age", 38); // "age" : 38

jGenerator.writeFieldName("messages"); // "messages" :

jGenerator.writeStartArray(); // [

jGenerator.writeString("msg 1"); // "msg 1"
jGenerator.writeString("msg 2"); // "msg 2"
jGenerator.writeString("msg 3"); // "msg 3"

jGenerator.writeEndArray(); // ]

jGenerator.writeEndObject(); // }

} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

Output

1
2
3
c:\\projects\\user.json

{"name":"mkyong","age":38,"messages":["msg 1","msg 2","msg 3"]}

2. JSON 数组写入 - JsonGenerator

2.1 把 JSON 数组写入到文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// JacksonExample2.java

package com.mkyong;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;

public class JacksonExample2 {

public static void main(String[] args) {

ObjectMapper mapper = new ObjectMapper();

try (JsonGenerator jGenerator =
mapper.getFactory().createGenerator(
new File("c:\\projects\\user2.json")
, JsonEncoding.UTF8)) {

// pretty print
jGenerator.useDefaultPrettyPrinter();

// start array
jGenerator.writeStartArray(); // [

jGenerator.writeStartObject(); // {

jGenerator.writeStringField("name", "mkyong"); // "name" : "mkyong"
jGenerator.writeNumberField("age", 38); // "age" : 38

jGenerator.writeFieldName("messages"); // "messages" :

jGenerator.writeStartArray(); // [

jGenerator.writeString("msg 1"); // "msg 1"
jGenerator.writeString("msg 2"); // "msg 2"
jGenerator.writeString("msg 3"); // "msg 3"

jGenerator.writeEndArray(); // ]

jGenerator.writeEndObject(); // }

// next object, pls

jGenerator.writeStartObject(); // {

jGenerator.writeStringField("name", "lap"); // "name" : "lap"
jGenerator.writeNumberField("age", 5); // "age" : 5

jGenerator.writeFieldName("messages"); // "messages" :

jGenerator.writeStartArray(); // [

jGenerator.writeString("msg a"); // "msg a"
jGenerator.writeString("msg b"); // "msg b"
jGenerator.writeString("msg c"); // "msg c"

jGenerator.writeEndArray(); // ]

jGenerator.writeEndObject(); // }

jGenerator.writeEndArray(); // ]

} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
}

Output

1
2
3
4
5
6
7
8
9
10
11
12
13
c:\\projects\\user2.json

[
{
"name" : "mkyong",
"age" : 38,
"messages" : [ "msg 1", "msg 2", "msg 3" ]
}, {
"name" : "lap",
"age" : 5,
"messages" : [ "msg a", "msg b", "msg c" ]
}
]

3. 读取 JSON - JsonParser

Token(标记)

在 Jackson 流媒体中,JSON 字符串会被分割成一个标记列表(token list),每个标记(token)都会被增量处理。

  • Token 1 = {
  • Token 2 = name
  • Token 3 = mkyong
  • Token 4 = }

3.1 JsonParser 解析 JSON 文件的例子:

1
2
3
c:\\projects\\user.json

{"name":"mkyong","age":38,"messages":["msg 1","msg 2","msg 3"]}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// JacksonExample3.java

package com.mkyong;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonMappingException;

import java.io.File;
import java.io.IOException;

public class JacksonExample3 {

public static void main(String[] args) {

try (JsonParser jParser = new JsonFactory()
.createParser(new File("c:\\projects\\user.json"));) {

// loop until token equal to "}"
while (jParser.nextToken() != JsonToken.END_OBJECT) {

String fieldname = jParser.getCurrentName();

if ("name".equals(fieldname)) {
// current token is "name",
// move to next, which is "name"'s value
jParser.nextToken();
System.out.println(jParser.getText());
}

if ("age".equals(fieldname)) {
jParser.nextToken();
System.out.println(jParser.getIntValue());
}

if ("messages".equals(fieldname)) {

if (jParser.nextToken() == JsonToken.START_ARRAY) {
// messages is array, loop until token equal to "]"
while (jParser.nextToken() != JsonToken.END_ARRAY) {
System.out.println(jParser.getText());
}
}

}

}

} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

}

Output

1
2
3
4
5
mkyong
38
msg 1
msg 2
msg 3

4. 读取 JSON 数组 - JsonParser

4.1 JsonParser 解析 JSON 数组文件的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
// c:\\projects\\user2.json

[
{
"name" : "mkyong",
"age" : 38,
"messages" : [ "msg 1", "msg 2", "msg 3" ]
}, {
"name" : "lap",
"age" : 5,
"messages" : [ "msg a", "msg b", "msg c" ]
}
]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// JacksonExample4.java

package com.mkyong;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonMappingException;

import java.io.File;
import java.io.IOException;

public class JacksonExample4 {

public static void main(String[] args) {

try (JsonParser jParser = new JsonFactory()
.createParser(new File("c:\\projects\\user2.json"));) {

// JSON array?
if (jParser.nextToken() == JsonToken.START_ARRAY) {

while (jParser.nextToken() != JsonToken.END_ARRAY) {

// loop until token equal to "}"
while (jParser.nextToken() != JsonToken.END_OBJECT) {

String fieldname = jParser.getCurrentName();
if ("name".equals(fieldname)) {
// current token is "name",
// move to next, which is "name"'s value
jParser.nextToken();
System.out.println(jParser.getText());
}

if ("age".equals(fieldname)) {
jParser.nextToken();
System.out.println(jParser.getIntValue());
}

if ("messages".equals(fieldname)) {

//jParser.nextToken(); // current token is "[", move next
if (jParser.nextToken() == JsonToken.START_ARRAY) {
// messages is array, loop until token equal to "]"
while (jParser.nextToken() != JsonToken.END_ARRAY) {
System.out.println(jParser.getText());
}
}

}

}

}
}

} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

}

Output

1
2
3
4
5
6
7
8
9
10
mkyong
38
msg 1
msg 2
msg 3
lap
5
msg a
msg b
msg c

Note
More Jackson examples