-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductsList.java
More file actions
86 lines (70 loc) · 2.68 KB
/
ProductsList.java
File metadata and controls
86 lines (70 loc) · 2.68 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
85
86
//import java.nio.channels.GatheringByteChannel;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class ProductsList {
public static void main(String[] args) {
try{
//String jsonString = "";
URL url = new URI("https://dummyjson.com/products").toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
StringBuilder response = new StringBuilder();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
}
String jsonString = response.toString();
JSONObject data = new JSONObject(jsonString);
JSONArray jsonArray = data.getJSONArray("products");
List<Product> products = new ArrayList<Product>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonProduct = jsonArray.getJSONObject(i);
Product product = new Product(
jsonProduct.getString("title"),
jsonProduct.getDouble("price"),
jsonProduct.getString("thumbnail")
);
products.add(product);
}
System.out.println(products.size());
for (int index = 0; index < products.size(); index++) {
Product product = products.get(index);
String output = "<" + (index + 1) + "> Product title: " + product.getTitle() + " price: " + product.getPrice();
System.out.println(output);
}
} catch (Exception e) {
System.out.println("Error occurred:" +e.getMessage());
}
}
}
class Product {
private String title;
private double price;
private String thumbnail;
public Product(String title, double price, String thumbnail) {
this.title = title;
this.price = price;
this.thumbnail = thumbnail;
}
public String getTitle() {
return title;
}
public double getPrice() {
return price;
}
public String getThumbnail() {
return thumbnail;
}
}