Search code examples
oracle-maf

How to convert the JSON array to List to create DataControl in oracle MCS?


Iam trying to create a List View for the below Json code

{"ImageList" :[

 {
  "ENO":"87",
  "ENAME" : "john",
   "EJOB":"clerk",
  },{
    "ENO":"21",
    "ENAME" : "Abdul",
    "EJOB":"Manager",
  } ]
}

This the DataControl Program This DataControl is invoked by another Class

 Runnable mcsJob = new Runnable(){
   public void run(){ 
   try {

           CustomAPI customApi = mobileBackend.getServiceProxyCustomApi();            
           MCSRequest request = new MCSRequest(mobileBackend.getMbeConfiguration());
           request.setConnectionName(mafConnection);            
           request.setRequestURI(requestURI);            
           request.setHttpMethod(httpMethod);            
           request.setPayload(payload==null?"":payload);
           request.setRetryLimit(0);
           HashMap<String,String> headers = new HashMap<String,String>();

           if(httpHeaders!=null)
           {
               headers.putAll(httpHeaders);
           }

           request.setHttpHeaders(headers);  
           MCSResponse response = customApi.sendForStringResponse(request);
           String jsonResponse = (String) response.getMessage(); 
           setEmployeeSearchResponse(jsonResponse);
           //Converting JSON string
           apiResponse.setEmpsearchResponse(employeeSearchResponse);
           JSONObject jsonObject = new JSONObject(apiResponse.getEmpsearchResponse());
           JSONObject bodyObject = jsonObject.getJSONObject("Body");
           JSONObject ProcessObject=bodyObject.getJSONObject("processResponse");
            JSONArray empObject=ProcessObject.getJSONArray("ImageList"); 

           for(int i=0;i<empObject.length();i++)

               {

                JSONObject js = empObject.getJSONObject(i); 
                String name= ""+js.getString("ENO");
                String photo = ""+js.getString("ENAME");
                String empno=""+js.getString("EJOB");
                 EmployeeSearchPOJO  empo=new EmployeeSearchPOJO();
                  empo.setEMPNO(empno);
                   empo.setENAME(name);
                   empo.setPHOTO(photo);   
                   employeeList.add(empo);

               }

      }     

Then I will return the List

I have created EmployeeSearchPOJO class

List I have created is

List<EmployeeSearchPOJO> employeeList=new ArrayList<EmployeeSearchPOJO>();

public void setEmployeeList(List<EmployeeSearchPOJO> employeeList) {
    this.employeeList = employeeList;
}

public List<EmployeeSearchPOJO> getEmployeeList() {
    return employeeList;
}

Solution

  • Step1

    create a table with columns as ENO,ENAME,EJOB.

    Step2

    Create a POJO Class with response variables. Generate get and set methods.

    Step 3

    Parse the json response and store it in to database.(This will increate the performance)

    Step4

    Create a class which is to be acts as datacontroll and create an arraylist of the type of pojo class. Generate get and set methods for the arraylist. In set method use providerChangeSupport . Write a method return which will fetch the values from the database and store the result (returnType is list) in to the setter method of the arraylist. Call the method from the constructor of the dc class.

    Step5

    Finally right click on the class and select CreateDataControll. Thenn from the application window of jdeveloper you can see the list in the Data Controlls panel.