-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonParser.java
More file actions
27 lines (22 loc) · 946 Bytes
/
JsonParser.java
File metadata and controls
27 lines (22 loc) · 946 Bytes
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
import org.json.JSONArray;
import org.json.JSONObject;
public class JsonParser {
public static void main(String[] args) {
String json = "[{\"title\":\"Book 1\",\"price\":10,\"thumbnail\":\"thumbnail1.jpg\"}]";
try {
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String title = jsonObject.getString("title");
int price = jsonObject.getInt("price");
String thumbnail = jsonObject.getString("thumbnail");
System.out.println("Title: " + title);
System.out.println("Price: " + price);
System.out.println("Thumbnail: " + thumbnail);
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}