Properties Methods Events Config Options Direct Link

    DataReader
      JsonReader

# Class Ext.data.JsonReader

Package: | Ext.data
Defined In: | JsonReader.js
Class: | JsonReader
Subclasses: | ArrayReader
Extends: | DataReader

Data reader class to create an Array of Ext.data.Record objects from a JSON response based on mappings in a provided Ext.data.Record constructor.

Example code:

    var Employee = Ext.data.Record.create([
        {name: 'name', mapping: 'name'},     // "mapping" property not needed if it's the same as "name"
        {name: 'occupation'}                 // This field will use "occupation" as the mapping.
    ]);
    var myReader = new Ext.data.JsonReader({
        totalProperty: "results",    // The property which contains the total dataset size (optional)
        root: "rows",                // The property which contains an Array of row objects
        id: "id"                     // The property within each row object that provides an ID for the record (optional)
    }, Employee);

This would consume a JSON file like this:

    { 'results': 2, 'rows': [
        { 'id': 1, 'name': 'Bill', occupation: 'Gardener' },
        { 'id': 2, 'name': 'Ben', occupation: 'Horticulturalist' } ]
    }

It is possible to change a JsonReader's metadata at any time by including a `MetaData` property in the data object. If this is detected in the object, a Store object using this Reader will fire its metachange event.

The `MetaData` property may contain any of the configuration options for this class. Additionally, it may contain a `fields` property which the JsonReader will use that as an argument to Ext.data.Record.create to configure the layout of the Records which it will produce.

Using the `MetaData` property, and the Store's metachange event, it is possible to have a Store-driven control initialize itself. The metachange event handler may interrogate the `MetaData` property (which may contain any user-defined properties needed) and the `MetaData.fields` property to perform any configuration required.

To use this facility to send the same data as the above example without having to code the creation of the Record constructor, you would create the JsonReader like this:

    var myReader = new Ext.data.JsonReader();

The first data packet from the server would configure the reader by containing a metaData property as well as the data:

    {
      'metaData': {
        totalProperty: 'results',
        root: 'rows',
        id: 'id',
        fields: [
          {name: 'name'},
          {name: 'occupation'} ]
       },
      'results': 2, 'rows': [
        { 'id': 1, 'name': 'Bill', occupation: 'Gardener' },
        { 'id': 2, 'name': 'Ben', occupation: 'Horticulturalist' } ]
    }

## Config Options

Config Options | Defined By
--- | ---
| id : String Name of the property within a row object that contains a record identifier value. | JsonReader
| root : String name of the property which contains the Array of row objects. | JsonReader
| successProperty : String Name of the property from which to retrieve the success attribute used by forms. | JsonReader
| totalProperty : String Name of the property from which to retrieve the total number of records in the dataset. This is only needed if the wh... Name of the property from which to retrieve the total number of records in the dataset. This is only needed if the whole dataset is not passed in one go, but is being paged from the remote server. | JsonReader

## Public Properties

Property | Defined By
--- | ---
| jsonData : Object After any data loads, the raw JSON data is available for further custom processing. If no data is loaded or there is... After any data loads, the raw JSON data is available for further custom processing. If no data is loaded or there is a load exception this property will be undefined. | JsonReader
| meta : Mixed This JsonReader's metadata as passed to the constructor, or as passed in the last data packet's `MetaData` property. | JsonReader

## Public Methods

Method | Defined By
--- | ---
| JsonReader( `Object meta`, `Object recordType` ) Create a new JsonReader Create a new JsonReader Parameters: `meta` : Object Metadata configuration options. `recordType` : Object Either an Array of field definition objects as passed to Ext.data.Record.create, or a Record constructor created using Ext.data.Record.create. Returns: `` | JsonReader
| read( `Object response` ) : Object This method is only used by a DataProxy which has retrieved data from a remote server. This method is only used by a DataProxy which has retrieved data from a remote server. Parameters: `response` : Object The XHR object which contains the JSON data in its responseText. Returns: `Object` data A data block which is used by an Ext.data.Store object as a cache of Ext.data.Records. | JsonReader
| readRecords( `Object o` ) : Object Create a data block containing Ext.data.Records from a JSON object. Create a data block containing Ext.data.Records from a JSON object. Parameters: `o` : Object An object which contains an Array of row objects in the property specified in the config as 'root, and optionally a property, specified in the config as 'totalProperty' which contains the total size of the dataset. Returns: `Object` data A data block which is used by an Ext.data.Store object as a cache of Ext.data.Records. | JsonReader

## Public Events

This class has no public events.