-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJason-javaobjects creation.txt
More file actions
58 lines (48 loc) · 1.64 KB
/
Jason-javaobjects creation.txt
File metadata and controls
58 lines (48 loc) · 1.64 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
import org.json.JSONArray;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
String jsonString = "[\n" +
" {\n" +
" \"title\": \"Product 1\",\n" +
" \"price\": 19.99,\n" +
" \"thumbnail\": \"product1.jpg\"\n" +
" },\n" +
" {\n" +
" \"title\": \"Product 2\",\n" +
" \"price\": 29.99,\n" +
" \"thumbnail\": \"product2.jpg\"\n" +
" }\n" +
"]";
JSONArray jsonArray = new JSONArray(jsonString);
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")
);
String output = "<" + (i + 1) + "> Product title: " + product.getTitle() + " price: " + product.getPrice();
System.out.println(output);
}
}
}
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;
}
}