Code to Parse a JSONObject having values in 'Object','Array',and we r going to parse that 'Object' and 'Array' and insert those values in Database

///ClsError Response that displays in Eclipse LogCat as followssss
01-25 09:50:23.911: I/System.out(708): {"clsError":{"ErrorCode":704,"ErrorDescription":"Case Object Available"},"clsCaseObject":{"clsCase":{"CaseID":24,"FirmID":7,"EmployeeID":13,"CaseStatus":3,"CreationDate":"1/24/2012 1:05:27 AM","CourtDate":"1/12/2012 12:00:00 AM","CaseType":"TRAFFIC"},"CaseCriminalTicketLinksArray":null,"CaseTrafficTicketLinksArray":[{"CasetrafficTicketlinkID":16,"CaseID":24,"CourtID":1340,"TicketNumber":"110292","TicketExtension":"P02","CourtDate":"1/12/2012 12:00:00 AM","LastUpdateTimeStamp":"1/12/2012 12:00:00 AM"}],"FilesArray":[{"FileID":33,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%221327388758657.jpg%22,%22Notes%22:%22%22,%22UploadDate%22:%221/24/2012 2:06:36 AM","IsDeleted":false},{"FileID":35,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%22null.jpeg%22,%22Notes%22:%22gjjgjggkkjjkkmbnkmm/n/n/n/n/n/nyity/nijhghh%22,%22UploadDate%22:%221/24/2012 3:47:33 AM","IsDeleted":false},{"FileID":37,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%22null.jpeg%22,%22Notes%22:%22added through emulator from android","UploadDate":"1/24/2012 5:22:05 AM","IsDeleted":false},{"FileID":38,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%22003bb44b981720685c04f95ff909c9aae5b8e4611740124201252916AM.jpeg%22,%22Notes%22:%22hio from","UploadDate":"1/24/2012 5:29:55 AM","IsDeleted":false},{"FileID":40,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%22003bb44b981720685c04f95ff909c9aae5b8e4611740124201253901AM.jpeg%22,%22Notes%22:%22%22,%22UploadDate%22:%221/24/2012 5:41:09 AM","IsDeleted":false},{"FileID":41,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%22003bb44b981720685c04f95ff909c9aae5b8e4611740124201255158AM.jpeg%22,%22Notes%22:%22varmna is added android image to JOR","UploadDate":"1/24/2012 5:53:32 AM","IsDeleted":false}],"NotesArray":[{"NoteID":58,"CaseID":24,"EmployeeID":7,"Note":"tilak tilak ti;lak","CreationDate":"1/24/2012 1:59:41 AM"},{"NoteID":59,"CaseID":24,"EmployeeID":7,"Note":"@#$#0$#0$#0$#","CreationDate":"1/24/2012 2:00:15 AM"},{"NoteID":60,"CaseID":24,"EmployeeID":7,"Note":"jksdhf khdsk fkj dsdhs","CreationDate":"1/24/2012 2:33:39 AM"},{"NoteID":69,"CaseID":24,"EmployeeID":7,"Note":"hi krishna","CreationDate":"1/24/2012 5:47:10 AM"},{"NoteID":70,"CaseID":24,"EmployeeID":7,"Note":"hi android dev how r u hd","CreationDate":"1/24/2012 5:53:01 AM"}]}}</GetCaseByCaseIDResult></Ge01-25 09:50:23.911: I/System.out(708): {"clsError":{"ErrorCode":704,"ErrorDescription":"Case Object Available"},"clsCaseObject":{"clsCase":{"CaseID":24,"FirmID":7,"EmployeeID":13,"CaseStatus":3,"CreationDate":"1/24/2012 1:05:27 AM","CourtDate":"1/12/2012 12:00:00 AM","CaseType":"TRAFFIC"},"CaseCriminalTicketLinksArray":null,"CaseTrafficTicketLinksArray":[{"CasetrafficTicketlinkID":16,"CaseID":24,"CourtID":1340,"TicketNumber":"110292","TicketExtension":"P02","CourtDate":"1/12/2012 12:00:00 AM","LastUpdateTimeStamp":"1/12/2012 12:00:00 AM"}],"FilesArray":[{"FileID":33,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%221327388758657.jpg%22,%22Notes%22:%22%22,%22UploadDate%22:%221/24/2012 2:06:36 AM","IsDeleted":false},{"FileID":35,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%22null.jpeg%22,%22Notes%22:%22gjjgjggkkjjkkmbnkmm/n/n/n/n/n/nyity/nijhghh%22,%22UploadDate%22:%221/24/2012 3:47:33 AM","IsDeleted":false},{"FileID":37,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%22null.jpeg%22,%22Notes%22:%22added through emulator from android","UploadDate":"1/24/2012 5:22:05 AM","IsDeleted":false},{"FileID":38,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%22003bb44b981720685c04f95ff909c9aae5b8e4611740124201252916AM.jpeg%22,%22Notes%22:%22hio from","UploadDate":"1/24/2012 5:29:55 AM","IsDeleted":false},{"FileID":40,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%22003bb44b981720685c04f95ff909c9aae5b8e4611740124201253901AM.jpeg%22,%22Notes%22:%22%22,%22UploadDate%22:%221/24/2012 5:41:09 AM","IsDeleted":false},{"FileID":41,"CaseID":24,"EmployeeID":7,"FilePath":"http://2.dharani.org/files/%22,%22FileTitle%22:%22003bb44b981720685c04f95ff909c9aae5b8e4611740124201255158AM.jpeg%22,%22Notes%22:%22varmna is added android image to JOR","UploadDate":"1/24/2012 5:53:32 AM","IsDeleted":false}],"NotesArray":[{"NoteID":58,"CaseID":24,"EmployeeID":7,"Note":"tilak tilak ti;lak","CreationDate":"1/24/2012 1:59:41 AM"},{"NoteID":59,"CaseID":24,"EmployeeID":7,"Note":"@#$#0$#0$#0$#","CreationDate":"1/24/2012 2:00:15 AM"},{"NoteID":60,"CaseID":24,"EmployeeID":7,"Note":"jksdhf khdsk fkj dsdhs","CreationDate":"1/24/2012 2:33:39 AM"},{"NoteID":69,"CaseID":24,"EmployeeID":7,"Note":"hi krishna","CreationDate":"1/24/2012 5:47:10 AM"},{"NoteID":70,"CaseID":24,"EmployeeID":7,"Note":"hi android dev how r u hd","CreationDate":"1/24/2012 5:53:01 AM"}]}}</GetCaseByCaseIDResult></Ge>


////Android code to parse above Response as follows

////code if u want to parse JsonObject exists on above Responce
JSONObject tempJson = new JSONObject(temp); 
        
      JSONObject clsErrorJson = tempJson.getJSONObject("clsError");
      JSONObject clsCase = tempJson.getJSONObject("clsCaseObject");
     
      clsError errorObj= new clsError(clsErrorJson.getInt("ErrorCode"),clsErrorJson.getString("ErrorDescription"));
        Log.d("Error code",""+errorObj.ErrorCode);
       
        JSONObject a=clsCase.getJSONObject("clsCase");
        clsCase clsCaseObj=new clsCase(a.getInt("CaseID"), a.getInt("FirmID"),a.getInt("EmployeeID"),a.getInt("CaseStatus"), a.getString("CreationDate"), a.getString("CourtDate"), a.getString("CaseType"),"");
        ///System.out.println("clsObjjjjjjj"+clsCaseObj.toString());
        System.out.println("clsObjjjjjjj"+clsCaseObj.CaseID);
        //Details_TrafficCasesFrag DetailsTraficobj=new Details_TrafficCasesFrag();
        DataBaseHelper userdb=new DataBaseHelper(context);///DetailsTraficobj.getActivity());
     SQLiteDatabase myDataBase=userdb.getReadableDatabase();
     boolean isExists=userdb.recordExistsOrNot(clsCaseObj.CaseID, "tblCase", "CaseID");
     if(isExists){//if record exists then delete it from Database(because it has fe changes in its data) and insert Updated row in DB
      userdb.deletRecord("CaseID", "tblCase", clsCaseObj.CaseID, isExists);
      clsCaseObj.writeCaseToDBWithId(clsCaseObj.CaseID, context);
     }
     else clsCaseObj.writeCaseToDBWithId(clsCaseObj.CaseID, context);

///code if u want to parse JsonArray data Exixsts on above response
JSONArray the_json_array = clsCase.getJSONArray("CaseTrafficTicketLinksArray");
        int size = the_json_array.length();
        JSONObject jsonObj;
       // ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
        for (int i = 0; i < size; i++) {
        /// JSONObject another_json_object=new JSONObject();
          jsonObj = the_json_array.getJSONObject(i);
         //String idddd = another_json_object.get("TicketNumber").toString();        
           // System.out.println("++another_json_object+++"+idddd);
            System.out.println("++another_json_object+++"+jsonObj.get("CasetrafficTicketlinkID").toString());
            System.out.println("++another_json_object+++"+jsonObj.getInt("CasetrafficTicketlinkID"));
            isExists=userdb.recordExistsOrNot(jsonObj.getInt("CasetrafficTicketlinkID"), "tblCaseTrafficTicketLink", "CaseTrafficTicketLinkID");
            clsCaseTrafficTicketLink c = new clsCaseTrafficTicketLink( jsonObj.getInt("CasetrafficTicketlinkID"),
              jsonObj.getInt("CaseID"),
              jsonObj.getInt("CourtID"),
              jsonObj.getString("TicketNumber"),
            jsonObj.getString("TicketExtension"),
           jsonObj.getString("CourtDate"),
           jsonObj.getString("LastUpdateTimeStamp"));
            if(isExists){//if record exists then delete it from Database(because it has fe changes in its data) and insert Updated row in DB
       userdb.deletRecord("CaseTrafficTicketLinkID", "tblCaseTrafficTicketLink", jsonObj.getInt("CasetrafficTicketlinkID"), isExists);
       c.writeCaseTrafficTicketLinksToDB(context);      
      }
      else c.writeCaseTrafficTicketLinksToDB(context);

Above code used in 'wsGetCaseByCaseID.java' file on 'JORForTabbedPhone' Project

Comments

Popular posts from this blog

Custom Dialog with no title and full screen in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard

Best working code to set Date limits on Date Picker Dialog,in Android