How to read JSONObject and JSONArrays from file and parse JSONObject and JSONArrays in java.

Download JSON jar and add desired project
JSON Input
 {  
  "name": "Hiro Mia",  
  "age": 30,  
  "address": {  
   "presentaddress": {  
    "streetAddress": "H#137 Sector#6, Uttara",  
    "city": "Dhaka",  
    "Country": "Bangladesh"  
   },  
   "permanentaddress": {  
    "streetAddress": "H#110 Road#10, Upshohor",  
    "city": "Sylhet",  
    "Country": "Bangladesh"  
   },  
   "officetaddress": {  
    "streetAddress": "H#11 Sector#13, Uttara",  
    "city": "Dhaka",  
    "Country": "Bangladesh"  
   }  
  },  
  "phoneNumber": [  
   {  
    "type": "home",  
    "number": "812-333-1111"  
   },  
   {  
    "type": "fax",  
    "number": "746-444-2222"  
   }  
  ],  
  "qualification": {  
   "Type": "B.Sc",  
   "institution": "SUST",  
   "Grade": "B+"  
  }  
 }  
Java Program
 import org.json.JSONArray;  
 import org.json.JSONException;  
 import org.json.JSONObject;  
 import java.io.BufferedReader;  
 import java.io.FileReader;  
 import java.io.IOException;  
 public class Parsejson {  
   public static void main(String[] agr) {  
     String jsondata = "";  
     BufferedReader br = null;  
     String str;  
     try {  
       br = new BufferedReader(new FileReader("JSON Example.txt"));  
       while ((str = br.readLine()) != null) {  
         jsondata += str;  
       }  
     } catch (IOException e) {  
       e.printStackTrace();  
     } finally {  
       try {  
         if (br != null)  
           br.close();  
       } catch (IOException ex) {  
         ex.printStackTrace();  
       }  
     }  
     System.out.println("JSON Output     : "+jsondata);
  
     try {  

       JSONObject obj = new JSONObject(jsondata);  
       System.out.println("Name        : " + obj.get("name"));

  
       JSONObject address = (JSONObject) (obj.get("address"));  
       System.out.println("Address       : " + address.toString());
  

       JSONObject permanentaddress = (JSONObject) (address.get("permanentaddress"));  
       System.out.println("Permanent address  : " + permanentaddress.toString());  
       System.out.println("Street Address   : " + permanentaddress.get("streetAddress"));  


       JSONArray phoneNumber = (JSONArray) obj.get("phoneNumber");  
       System.out.println("Phone Number    : " + phoneNumber.toString());  
       System.out.println("Phone Number one  : " + phoneNumber.get(0).toString());

  
       System.out.println("Size of JSON Array : " + phoneNumber.length());  
       for (int i = 0; i < phoneNumber.length(); i++) {  
         System.out.println("Phone Number    : " + i + "  " + phoneNumber.get(i).toString());  
         JSONObject phoneNumber2 = (JSONObject) (phoneNumber.get(i));  
         System.out.println("Number       : " + i + "  " + phoneNumber2.get("number"));  
       }  


       JSONObject qualification = (JSONObject) (obj.get("qualification"));  
       System.out.println("Qualification    : " + qualification.toString());  
       System.out.println("Institution     : " + qualification.get("institution"));  


     } catch (JSONException e) {  
       e.printStackTrace();  
     }  
   }  
 }  

Output




 JSON Output        : { "name": "Hiro Mia", "age": 30, "address": {  "presentaddress": {   "streetAddress": "H#137 Sector#6, Uttara",   "city": "Dhaka",   "Country": "Bangladesh"  },  "permanentaddress": {   "streetAddress": "H#110 Road#10, Upshohor",   "city": "Sylhet",   "Country": "Bangladesh"  },  "officetaddress": {   "streetAddress": "H#11 Sector#13, Uttara",   "city": "Dhaka",   "Country": "Bangladesh"  } }, "phoneNumber": [  {   "type": "home",   "number": "812-333-1111"  },  {   "type": "fax",   "number": "746-444-2222"  } ], "qualification": {  "Type": "B.Sc",  "institution": "SUST",  "Grade": "B+" }} 
 
 Name               : Hiro Mia  
 Address            : {"permanentaddress":{"streetAddress":"H#110 Road#10, Upshohor","city":"Sylhet","Country":"Bangladesh"},"officetaddress":{"streetAddress":"H#11 Sector#13, Uttara","city":"Dhaka","Country":"Bangladesh"},"presentaddress":{"streetAddress":"H#137 Sector#6, Uttara","city":"Dhaka","Country":"Bangladesh"}}  

 Permanent address  : {"streetAddress":"H#110 Road#10, Upshohor","city":"Sylhet","Country":"Bangladesh"}  
 Street Address     : H#110 Road#10, Upshohor  
 Phone Number       : [{"number":"812-333-1111","type":"home"},{"number":"746-444-2222","type":"fax"}]  
 Phone Number one   : {"number":"812-333-1111","type":"home"}  
 Size of JSON Array : 2  
 Phone Number       : 0  {"number":"812-333-1111","type":"home"}  
 Number             : 0  812-333-1111  
 Phone Number       : 1  {"number":"746-444-2222","type":"fax"}  
 Number             : 1  746-444-2222  
 Qualification      : {"institution":"SUST","Type":"B.Sc","Grade":"B+"}  
 Institution        : SUST  

No comments:

Post a Comment