Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
120 lines (103 loc) · 3.74 KB

File metadata and controls

120 lines (103 loc) · 3.74 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class JsonParseTest {
private static final String filePath = "//home//user//Documents//jsonDemoFile.json";
public static void main(String[] args) {
try {
// read the json file
FileReader reader = new FileReader(filePath);
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject)jsonParser.parse(reader);
// get a number from the JSON object
Long id = (Long) jsonObject.get("id");
System.out.println("The id is: " + id);
// get a String from the JSON object
String type = (String) jsonObject.get("type");
System.out.println("The type is: " + type);
// get a String from the JSON object
String name = (String) jsonObject.get("name");
System.out.println("The name is: " + name);
// get a number from the JSON object
Double ppu = (Double) jsonObject.get("ppu");
System.out.println("The PPU is: " + ppu);
// get an array from the JSON object
System.out.println("Batters:");
JSONArray batterArray= (JSONArray) jsonObject.get("batters");
Iterator i = batterArray.iterator();
// take each value from the json array separately
while (i.hasNext()) {
JSONObject innerObj = (JSONObject) i.next();
System.out.println("ID "+ innerObj.get("id") +
" type " + innerObj.get("type"));
}
// get an array from the JSON object
System.out.println("Topping:");
JSONArray toppingArray= (JSONArray) jsonObject.get("topping");
Iterator j = toppingArray.iterator();
// take each value from the json array separately
while (j.hasNext()) {
JSONObject innerObj = (JSONObject) j.next();
System.out.println("ID "+ innerObj.get("id") +
" type " + innerObj.get("type"));
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ParseException ex) {
ex.printStackTrace();
} catch (NullPointerException ex) {
ex.printStackTrace();
}
}
}
jsonDemoFile.json:
====================
{
"id": 0001,
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
[
{ "id": 1001, "type": "Regular" },
{ "id": 1002, "type": "Chocolate" },
{ "id": 1003, "type": "Blueberry" },
{ "id": 1004, "type": "Devil's Food" }
],
"topping":
[
{ "id": 5001, "type": "None" },
{ "id": 5002, "type": "Glazed" },
{ "id": 5005, "type": "Sugar" },
{ "id": 5007, "type": "Powdered Sugar" },
{ "id": 5006, "type": "Chocolate with Sprinkles" },
{ "id": 5003, "type": "Chocolate" },
{ "id": 5004, "type": "Maple" }
]
}
Output:
------------
The id is: 1
The type is: donut
The name is: Cake
The PPU is: 0.55
Batters:
ID 1001 type Regular
ID 1002 type Chocolate
ID 1003 type Blueberry
ID 1004 type Devil's Food
Topping:
ID 5001 type None
ID 5002 type Glazed
ID 5005 type Sugar
ID 5007 type Powdered Sugar
ID 5006 type Chocolate with Sprinkles
ID 5003 type Chocolate
ID 5004 type Maple
Morty Proxy This is a proxified and sanitized view of the page, visit original site.