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 - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Answers
    3932
    Vote Rating
    1272
      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
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    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
  •