-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
84 lines (71 loc) · 2.71 KB
/
Driver.java
File metadata and controls
84 lines (71 loc) · 2.71 KB
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
79
80
81
82
83
84
/**
* Read JSON from file using JsonParser
* @author Rohith Pudari
*
*/
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import java.io.File;
import java.io.IOException;
public class Driver {
public static void main(String[] args) throws IOException {
JsonFactory factory = new JsonFactory();
// Create Reader/InputStream/File
File file = new File("post.json");
// Create JsonParser
JsonParser parser = factory.createParser(file);
parser.nextToken();
JsonToken token; // Read first object i.e. {
// Read JSON object
token = parser.nextToken();
if (token == JsonToken.FIELD_NAME && "id".equals(parser.getCurrentName())) {
token = parser.nextToken();
if (token == JsonToken.VALUE_STRING) {
System.out.println("ID : " + parser.getText());
}
}
token = parser.nextToken();
if (token == JsonToken.FIELD_NAME && "title".equals(parser.getCurrentName())) {
token = parser.nextToken();
if (token == JsonToken.VALUE_STRING) {
System.out.println("title : " + parser.getText());
}
}
token = parser.nextToken();
if (token == JsonToken.FIELD_NAME && "description".equals(parser.getCurrentName())) {
token = parser.nextToken();
if (token == JsonToken.VALUE_STRING) {
System.out.println("description : " + parser.getText());
}
}
token = parser.nextToken();
if (token == JsonToken.FIELD_NAME && "content".equals(parser.getCurrentName())) {
token = parser.nextToken();
if (token == JsonToken.VALUE_STRING) {
System.out.println("content : " + parser.getText());
}
}
// Reading JSON Array
token = parser.nextToken();
if (token == JsonToken.FIELD_NAME && "tags".equals(parser.getCurrentName())) {
System.out.println("Post tags are - ");
token = parser.nextToken(); // // Read left bracket i.e. [
// Loop to print array elements until right bracket i.e ]
while (token != JsonToken.END_ARRAY) {
token = parser.nextToken();
if (token == JsonToken.VALUE_STRING) {
System.out.println(parser.getText());
}
}
System.out.println();
}
parser.close();
try {
Jsonobject jsonobject = new Jsonobject();
jsonobject.geocoding("Rohith-hacker");
} catch (Exception e) {
e.printStackTrace();
}
}
}