json data format code example

Example 1: json data example

[
	{
		"id": "0001",
		"type": "donut",
		"name": "Cake",
		"ppu": 0.55,
		"batters":
			{
				"batter":
					[
						{ "id": "1001", "type": "Regular" },
						{ "id": "1002", "type": "Chocolate" },
						{ "id": "1003", "type": "Blueberry" },
						{ "id": "1004", "type": "Devil's Food" }
					]
			},
		"topping":
			[
				{ "id": "5001", "type": "None" },
				{ "id": "5002", "type": "Glazed" },
				{ "id": "5005", "type": "Sugar" },
				{ "id": "5007", "type": "Powdered Sugar" },
				{ "id": "5006", "type": "Chocolate with Sprinkles" },
				{ "id": "5003", "type": "Chocolate" },
				{ "id": "5004", "type": "Maple" }
			]
	},
	{
		"id": "0002",
		"type": "donut",
		"name": "Raised",
		"ppu": 0.55,
		"batters":
			{
				"batter":
					[
						{ "id": "1001", "type": "Regular" }
					]
			},
		"topping":
			[
				{ "id": "5001", "type": "None" },
				{ "id": "5002", "type": "Glazed" },
				{ "id": "5005", "type": "Sugar" },
				{ "id": "5003", "type": "Chocolate" },
				{ "id": "5004", "type": "Maple" }
			]
	},
	{
		"id": "0003",
		"type": "donut",
		"name": "Old Fashioned",
		"ppu": 0.55,
		"batters":
			{
				"batter":
					[
						{ "id": "1001", "type": "Regular" },
						{ "id": "1002", "type": "Chocolate" }
					]
			},
		"topping":
			[
				{ "id": "5001", "type": "None" },
				{ "id": "5002", "type": "Glazed" },
				{ "id": "5003", "type": "Chocolate" },
				{ "id": "5004", "type": "Maple" }
			]
	}
]

Example 2: javascript object notation

{
  "first_name": "Taylor",
  "last_name": "Hawkes",
  "age": 31,
  "address": {
    "street": "954 Kazaam Lane",
    "city": "Boulder",
    "state": "CO",
    "postalCode": "80303"
  },
  "emails": [
    {
      "type": "main",
      "number": "[email protected]"
    },
    {
      "type": "secondary",
      "number": "[email protected]"
    }
  ]
}

Example 3: what is JSON

JavaScript Object Notation 
	Its a key value pair 
	its popular light-weight way of transfering data 

	for example : 
	Lets try to create a json for Person Object 
			with name , age , isMarried , gender 

		Its ket value pair 
		the key is always String and need to be in quotation
		value can be :
			String 
			Number 
			Boolean 
			null 
			array 
			another json object


		This is one json with 5 fields 
		each key value pair should be separated by comma 
		{
			"name" 		: "Anna", 
			"age" 		: 18  , 
			"isMarried" : false , 
			"gender"	: "female", 
			"company"	: null 
		}

Example 4: JSON stands for

JSON = JavaScript Object Notation

Example 5: JSON format

ArrayList<Countries> listCountries = new ArrayList<>();//country
        ArrayList<Detail> listDetails = new ArrayList<>();//listDetail
        ArrayList<Detail> listSub =  new ArrayList<>();//List Sub

        Countries countries = new Countries();
        countries.setId("1");
        countries.setName("Sim");
        countries.setGender("M");
        countries.setCountry("khompong Chhnang");
        countries.setPostalCode("225566");
        //Add Countries object to ArrayList
        listCountries.add(countries);

        Detail detail =  new Detail();
        detail.setPhone("09659694146");
        detail.setAddress("11H");
        //Add Detail object to ArrayList
        listDetails.add(detail);

        Detail detail1 =  new Detail();
        detail1.setPhone("2222");
        detail1.setAddress("tttt2");
        listSub.add(detail1);

        JSONObject jsonObject_sub = new JSONObject();
        JSONArray jsonArray_sub = new JSONArray();
        for (int i = 0; i < listSub.size(); i++){
            try {
                jsonObject_sub.put("Phone",listSub.get(i).getPhone());
                jsonObject_sub.put("Address",listSub.get(i).getAddress());
                jsonArray_sub.put(jsonObject_sub);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        JSONObject jb = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i<listDetails.size(); i++){
            //convert to JSONObject
            try {
                jb.put("Address",listDetails.get(i).getAddress());
                jb.put("Phone",listDetails.get(i).getPhone());
                jb.put("sub_detail",jsonArray_sub);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        //convert JSONObject to JSONArray
        for (int i = 0; i < 3; i++){
            jsonArray.put(jb);
        }


        JSONObject jsonObject = new JSONObject();
        for (int i=0; i<listCountries.size(); i++){
            try {
                //convert to JSONObject
                jsonObject.put("id",listCountries.get(i).getId());
                jsonObject.put("name",listCountries.get(i).getName());
                jsonObject.put("gender",listCountries.get(i).getGender());
                //put JSONArray into JSONObject
                jsonObject.put("detail",jsonArray);
                jsonObject.put("country",listCountries.get(i).getCountry());
                jsonObject.put("postal_code",listCountries.get(i).getPostalCode());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.d(">>>", "jsonObject: "+jsonObject);
        }


================Output===========================================================
  {"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"}