-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJson.java
More file actions
90 lines (70 loc) · 2.02 KB
/
Copy pathJson.java
File metadata and controls
90 lines (70 loc) · 2.02 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
87
88
89
90
package oops;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.util.Iterator;
public class Json
{
public static <JSONArray> void main(String[] args)
{
Json Rice = new Json();
Rice.put("name", "sharma Rice");
Rice.put("weight", "30");
Rice.put("price","1110");
Json rice1= new Json();
rice1.put("inventory", Rice);
Json Pulses = new Json();
Pulses.put("name", "red gram");
Pulses.put("weight", "5");
Pulses.put("price", "300");
Json pulses1= new Json();
pulses1.put("inventory", Pulses);
Json Wheat = new Json();
Wheat.put("name", "ATTA");
Wheat.put("weight", "25");
Wheat.put("price", "5000");
Json wheat1 = new Json();
wheat1.put("inventory", Wheat);
JSONArray jArray = new JSONArray();
jArray.add(rice1);
jArray.add(pulses1);
jArray.add(wheat1);
try (FileWriter fWriter = new FileWriter("Inventory.json"))
{
fWriter.write(jArray.toString());
fWriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONParser jsonParser = new JSONParser();
try (FileReader fr = new FileReader("Inventory.json"))
{
Object object = jsonParser.parse(fr);
JSONArray invntry = (JSONArray)object;
System.out.println(invntry);
invntry.forEach( emp -> parseData( (JSONObject) emp ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
private void put(String string, String string2) {
// TODO Auto-generated method stub
}
private void put(String string, Json pulses) {
// TODO Auto-generated method stub
}
public static void parseData(Json inv) {
Json inven = (Json)inv.get("inventory");
String name = (String)inven.get("name");
System.out.println(name);
String weight = (String)inven.get("weight");
System.out.println(weight);
String price = (String)inven.get("price");
System.out.println(price);