Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    best prac working with multi-level json rest response?

    I am using SmartGWT 2.2.

    I have a REST service that returns a multi-level JSON response. Furthermore, this is not a response that I am simply going to bind to a dynamic form or a grid. I have to do some custom processing of the resulting response. I have set up a Datasource object to issue the REST call via a "fetchData() method call with a DSCallback(). The DSCallback method provides this override:

    Code:
    @Override
    public void execute(DSResponse response, Object rawData, DSRequest request)
    Through some playing around, I've found that I can get to the javascript object one of two ways:

    1. Via the "rawData" object that is passed into the function, like this:

    Code:
    JavaScriptObject foo = ((JsArray<JavaScriptObject>) rawData).get(0);
    2. Via an element of the "response" object that is passed into the function, like this:
    Code:
    JavaScriptObject foo = response.getData()[0].getJsObj();
    Are either of the two above methods the correct way to do this? If not, what is the correct way? My eventual goal is to cast these javascript objects to Java overlay instances.

    Any help would be appreciated.

    Thanks,

    Alex
    Last edited by twelve17; 18 Nov 2010, 13:12. Reason: more descriptive subject

    #2
    Hi Alex,
    I use SmartGWT 2.3. But this works on 2.2 either.

    I just crack my head with this.
    And resolve using the "transformResponse" method.

    Like this:

    The JSON answer:
    Code:
    {"response":{"status":0,"data":{"user":{"groupname":"Manager","name":"Jo&atilde;o da Silva","langid":2,"Id":2,"operatorid":1,"groupid":"4"},"Id":1,"operator":{"Id":1,"nick":"OPER","name":"Operator Dummy S\/A","logo":"operator_logo.png"},
    "blocks":{"list":["Block 1","Block 2"],"n":2},"groups":{"list":["Normal User","Manager","Admin"],"n":3},"languages":{"list":[{"nick":"us","name":"English","id":"1","flag":"us.png","tag":"en"},{"nick":"br","name":"Portugu&ecirc;s Brasil","id":"2","flag":"br.png","tag":"pt_BR"},{"nick":"es","name":"Espa&ntilde;ol","id":"3","flag":"es.png","tag":"es"}],"n":3}}}}
    I use a RestDataSource with one field only, the "Id".

    Code:
        DataSourceTextField id = new DataSourceTextField("Id");
        id.setPrimaryKey(true);
    
        this.setFields(id);
    To get the other values, I implement the "transformResponse" method:

    Code:
      /**
       * Transform the response into Java code.
       */
      @Override
      protected void transformResponse(DSResponse response, DSRequest request, Object jsonData) {
        // Get the master record
        Record recordMaster = response.getData()[0];
    
        // ==================================
        // Load Languages List
        // ==================================
        try {
          Record record = recordMaster.getAttributeAsRecordArray("languages")[0];
          // Get the array of records
          Record[] langRecord = record.getAttributeAsRecordArray("list");
          // Allocate the space
          Languages = new Idiomes[langRecord.length];
          // Run all elements
          for(int i = 0; i < langRecord.length; i++) {
            Idiomes lang = new Idiomes();
            lang.tag  = langRecord[i].getAttributeAsString("tag");
            lang.name = langRecord[i].getAttributeAsString("name");
            lang.nick = langRecord[i].getAttributeAsString("nick");
            lang.flag = langRecord[i].getAttributeAsString("flag");
            Languages[i] = lang;
          }
        } catch (Exception e) {
          return;
        }
        
        // ==================================
        // Load Users Group List
        // ==================================
        try {
          Record record = recordMaster.getAttributeAsRecordArray("groups")[0];
          GroupList = record.getAttributeAsStringArray("list");
        } catch (Exception e) {
          return;
        }
    
        // ==================================
        // Load Blocks List
        // ==================================
        try {
          Record record = recordMaster.getAttributeAsRecordArray("blocks")[0];
          BlockList = record.getAttributeAsStringArray("list");
        } catch (Exception e) {
          return;
        }
    
       /**
        * Where:
        */
       String[] BlockList;
       String[] GroupList;
       Idiomes[] Languages;
    
       /**
        * System languages
        */
    public class Idiomes {
      public String tag;
      public String name;
      public String nick;
      public String flag;
      /**
       * Main constructor
       */
      public Idiomes() {
        tag = "";
        name = "";
        nick = "";
        flag = "";
      }
    }
    This works well for me.
    I hope that help you.
    I'm not sure if this is the best way.

    Best regards,
    Andre.

    Comment


      #3
      Hi Andre,

      Thanks for the detailed response. Sorry, I should have probably pointed out that I already figured out the part of actually getting access to parts of the record once I get it. I just wasn't sure if this part is the right way to get to the record itself:

      Code:
      Record recordMaster = response.getData()[0];
      Because the method also had the "rawData" parameter, with the object too.

      Cheers!

      Alex

      Comment


        #4
        Alex,

        I saw some samples, looking at posts and trying some codes, and I arrived on that solution.
        Because, the response struct just have all the rawData on it.

        The status and data fields, using the default Xpath, are processed by SmartGWT and
        you don't have access to then.

        So I use this to caught the others fields.

        I was clear?

        Cheers,
        Andre.

        Comment


          #5
          Hi Andre,

          Sorry for missing your reply. Yes, I think you were clear. Thanks for your help.

          Cheers,

          Alex

          Comment

          Working...
          X