Results 1 to 2 of 2

Thread: How to implement custom JSON reader

  1. #1
    Sencha User
    Join Date
    Apr 2013
    Posts
    15
    Answers
    1
    Vote Rating
    2
      2  

    Default Answered: How to implement custom JSON reader

    My ST2 app needs to consume JSON in an odd format. None of the examples in JSON and Reader docs give hints as for how to implement a custom reader.

    I have a model like so:
    Code:
    Ext.define('StationData', {
      extend: 'Ext.data.Model',
      fields: [
        {name: 'id', type: 'string'},
        {name: 'name', type: 'string'} ,
        {name: 'lat', type: 'string'},
        {name: 'long', type: 'string'},
        {name: 'foo', type: 'int'}
      ]
    });
    The JSON is like so:
    Code:
    { "66000" : // needs to be mapped to 'id'
        {
          "lat" : "nn.nnnn",
          "lon" : "nn.nnnn", // needs to be mapped to 'long'
          "name" : "Foo",
          "f" : "19", // needs to be mapped to 'foo'
          ...
        },
      "66010" : 
       {....
    How do I go about this?

  2. Extend the Json reader and use the getResponseData method

    Code:
    Ext.define('MyReader', {
        extend : 'Ext.data.reader.Json',
        alias : 'reader.myreader',
    
        getResponseData : function(response) {
            var data = this.callParent([response]);
    
            //do stuff here
    
            return data;
        }
    });

  3. #2
    Sencha User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,029
    Answers
    3976
    Vote Rating
    1367
      2  

    Default

    Extend the Json reader and use the getResponseData method

    Code:
    Ext.define('MyReader', {
        extend : 'Ext.data.reader.Json',
        alias : 'reader.myreader',
    
        getResponseData : function(response) {
            var data = this.callParent([response]);
    
            //do stuff here
    
            return data;
        }
    });
    Mitchell Simoens @LikelyMitch
    Modus Create, Senior Frontend Engineer
    ________________
    Need any sort of Ext JS help? Modus Create is here to help!

    Check out my GitHub:
    https://github.com/mitchellsimoens

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •