Thursday, December 13, 2012

How To Extract Values From A  Jason Array

In smartGWT we get the values to client side as a Jason array.It usually looks as below.





{"list":[{"destination":"MODESTO, CA",
          "destinationCity":"MODESTO",
          "destinationState":"CA",
          "id":573023,
          "miles":1419.0,
          "origin":"COUTTS, AB",
          "originCity":"COUTTS",
          "originState":"AB",
          "railRoad":"BNSF",
          "tableName":"BNSF MILES",
          "transportMode":"RAIL"}],
"recordCount":1,
"responseEndRow":1,
"responseErrors":{},
"responseStartRow":0,
"responseTime":1.515,
"responseTotalRows":1,
"responseWarns":[]}
 
Above is one example of a Jason array, how it will be shown once you install fire bug.
Once the action is sent, and reply is back, firebug allows us to see the Jason array 
response & it will look as above.
 
list consist of the domain object and it has one domain object with attributes such as 
destination, destinationCity etc.
 
If we have two objects in the array, it will look as below in the response.
 
{"list":[{"destination":"213 MILE SIDING, VA",
          "destinationCity":"213 MILE SIDING",
          "destinationState":"VA",
          "id":179570,
          "miles":770.2,
          "origin":"CHICAGO, IL",
          "originCity":"CHICAGO",
          "originState":"IL",
          "railRoad":"NS",
          "tableName":"RATE BASIS"},
 
          {"destination":"213 MILE SIDING, VA",
           "destinationCity":"213 MILE SIDING",
           "destinationState":"VA",
           "id":189956,
           "miles":447.8,
           "origin":"CINCINNATI, OH",
           "originCity":"CINCINNATI",
           "originState":"OH",
           "railRoad":"NS",
           "tableName":"RATE BASIS"}],
 
           "recordCount":2,
           "responseEndRow":2,
           "responseErrors":{},
           "responseStartRow":0,
           "responseTime":1.86,
           "responseTotalRows":2,
           "responseWarns":[]} 

These attributes can be extracted the normal way using a data source.But you need to have
 the attributes in the domain object.
 
private DistanceTablesDS() {
        setRecordXPath("/list");
        setDataFormat(DSDataFormat.JSON);
        setShowPrompt(false);
        setDataURL("distanceTablesSupport!fetchDistanceTablesList.action");
        setFetchDataURL("distanceTablesSupport!fetchDistanceTablesList.action");
        setAddDataURL("distanceTablesSupport!addDistanceTables.action");
        setUpdateDataURL("distanceTablesSupport!updateDistanceTables.action");
        setRemoveDataURL("distanceTablesSupport!deleteDistanceTables.action");

        DataSourceTextField dfId = new DataSourceTextField(FieldName.ID, FieldTitle.ID);
        dfId.setPrimaryKey(true);

        DataSourceTextField dfTableName = new DataSourceTextField(FieldName.TABLE_NAME, FieldTitle.TABLE_NAME);
        DataSourceTextField dfOriginCity = new DataSourceTextField(FieldName.ORIGIN_CITY, FieldTitle.ORIGIN_CITY);
        DataSourceTextField dfOriginState = new DataSourceTextField(FieldName.ORIGIN_STATE, FieldTitle.ORIGIN_STATE);
       

        setFields(dfId, dfTableName, dfOriginCity, dfOriginState);

    }

But if you do not have the attribute in the domain object or if you do not have the attribute as a data Source field in the data source, then you can extract values from the Jason array as below and it should be within data source at client side.

(Some such attributes in above example are responseTime, recordCount, responseTotalRows etc.)

public class CostRequestDS extends RatesDataSource {

 private static CostRequestDS INSTANCE = null;

 private String responseTotalRows;
 private String responseTime;

private CostRequestDS() {......}

public static CostRequestDS getInstance() {
  if (INSTANCE == null) {
   INSTANCE = new CostRequestDS();
  }
  return INSTANCE;
}
 
protected void transformResponse(DSResponse response, DSRequest request, Object jsonData) {
  super.transformResponse(response, request, jsonData);

  JSONArray jsonArray = XMLTools.selectObjects(jsonData,
    shipxpress.rates3.client.common.Constant.FieldName.RESPONSE_TOTAL_ROWS);
  JSONArray jsonArray2 = XMLTools.selectObjects(jsonData,
    shipxpress.rates3.client.common.Constant.FieldName.RESPONSE_TIME);

  if (jsonArray != null && jsonArray.size() > 0) {
   responseTotalRows = jsonArray.get(0).isNumber().toString();
  }

  if (jsonArray2 != null && jsonArray2.size() > 0) {
   responseTime = jsonArray2.get(0).isNumber().toString();
  }
}
 
public String getResponseTime() {
  return responseTime; 
}
 
public String getResponseTotalRows() {
  return responseTotalRows; 
}

}
 
Then at view panel we can show the value as below.
 
 grid.fetchData(criteria, new DSCallback() {
    @Override
    public void execute(DSResponse response, Object rawData, DSRequest request) {
     sectionList.setTitle("List :&nbsp&nbsp&nbsp&nbsp&nbsp"
       + Constant.NumberFormater.GENERAL_NUMBER_FORMAT.format(Double.parseDouble(dsGrid
         .getRecordCount().toString())) + "&nbsp record(s) found - Response Time: "
       + dsGrid.getResponseTime() + "(s)");
     grid.markForRedraw("redraw");
    }
        });