1{
2 "first_name": "Taylor",
3 "last_name": "Hawkes",
4 "age": 31,
5 "address": {
6 "street": "954 Kazaam Lane",
7 "city": "Boulder",
8 "state": "CO",
9 "postalCode": "80303"
10 },
11 "emails": [
12 {
13 "type": "main",
14 "number": "th71852@gmail.com"
15 },
16 {
17 "type": "secondary",
18 "number": "taylordhawkes@gmail.com"
19 }
20 ]
21}
1{
2 "naming": "Umair Ali",
3 "fee": 4567.8,
4 "rollNo": 33,
5 "fruits": [
6 "banana",
7 "apple",
8 "orange"
9 ],
10 "nesting": {
11 "school": "Paras",
12 "class": 9,
13 "myObj": [
14 "english",
15 "history",
16 "science",
17 65.4,
18 {
19 "boolean": true
20 },
21 "InternetKiDunya.Com"
22 ]
23 }
24}
1JavaScript Object Notation
2 Its a key value pair
3 its popular light-weight way of transfering data
4
5 for example :
6 Lets try to create a json for Person Object
7 with name , age , isMarried , gender
8
9 Its ket value pair
10 the key is always String and need to be in quotation
11 value can be :
12 String
13 Number
14 Boolean
15 null
16 array
17 another json object
18
19
20 This is one json with 5 fields
21 each key value pair should be separated by comma
22 {
23 "name" : "Anna",
24 "age" : 18 ,
25 "isMarried" : false ,
26 "gender" : "female",
27 "company" : null
28 }
1var myObj, x;
2myObj = {"name":"John", "age":30, "car":null};
3x = myObj.name;
4document.getElementById("demo").innerHTML = x;
1ArrayList<Countries> listCountries = new ArrayList<>();//country
2 ArrayList<Detail> listDetails = new ArrayList<>();//listDetail
3 ArrayList<Detail> listSub = new ArrayList<>();//List Sub
4
5 Countries countries = new Countries();
6 countries.setId("1");
7 countries.setName("Sim");
8 countries.setGender("M");
9 countries.setCountry("khompong Chhnang");
10 countries.setPostalCode("225566");
11 //Add Countries object to ArrayList
12 listCountries.add(countries);
13
14 Detail detail = new Detail();
15 detail.setPhone("09659694146");
16 detail.setAddress("11H");
17 //Add Detail object to ArrayList
18 listDetails.add(detail);
19
20 Detail detail1 = new Detail();
21 detail1.setPhone("2222");
22 detail1.setAddress("tttt2");
23 listSub.add(detail1);
24
25 JSONObject jsonObject_sub = new JSONObject();
26 JSONArray jsonArray_sub = new JSONArray();
27 for (int i = 0; i < listSub.size(); i++){
28 try {
29 jsonObject_sub.put("Phone",listSub.get(i).getPhone());
30 jsonObject_sub.put("Address",listSub.get(i).getAddress());
31 jsonArray_sub.put(jsonObject_sub);
32 } catch (JSONException e) {
33 e.printStackTrace();
34 }
35 }
36
37 JSONObject jb = new JSONObject();
38 JSONArray jsonArray = new JSONArray();
39 for (int i = 0; i<listDetails.size(); i++){
40 //convert to JSONObject
41 try {
42 jb.put("Address",listDetails.get(i).getAddress());
43 jb.put("Phone",listDetails.get(i).getPhone());
44 jb.put("sub_detail",jsonArray_sub);
45 } catch (JSONException e) {
46 e.printStackTrace();
47 }
48 }
49 //convert JSONObject to JSONArray
50 for (int i = 0; i < 3; i++){
51 jsonArray.put(jb);
52 }
53
54
55 JSONObject jsonObject = new JSONObject();
56 for (int i=0; i<listCountries.size(); i++){
57 try {
58 //convert to JSONObject
59 jsonObject.put("id",listCountries.get(i).getId());
60 jsonObject.put("name",listCountries.get(i).getName());
61 jsonObject.put("gender",listCountries.get(i).getGender());
62 //put JSONArray into JSONObject
63 jsonObject.put("detail",jsonArray);
64 jsonObject.put("country",listCountries.get(i).getCountry());
65 jsonObject.put("postal_code",listCountries.get(i).getPostalCode());
66 } catch (JSONException e) {
67 e.printStackTrace();
68 }
69 Log.d(">>>", "jsonObject: "+jsonObject);
70 }
71
72
73================Output===========================================================
74 {"id":"1","name":"Sim","gender":"M","detail":[{"Address":"11H","Phone":"09659694146","sub_detail":[{"Phone":"2222","Address":"tttt2"}]},{"Address":"11H","Phone":"09659694146","sub_detail":[{"Phone":"2222","Address":"tttt2"}]},{"Address":"11H","Phone":"09659694146","sub_detail":[{"Phone":"2222","Address":"tttt2"}]}],"country":"khompong Chhnang","postal_code":"225566"}