The JSON Reader is used by a Proxy to read a server response that is sent back in JSON format. This usually happens as a result of loading a Store - for example we might create something like this:(JSON Reader被Proxy用来读取服务器回应的JSON格式的数据。这经常在加载Store的发生。例如我们创建一个像下面的东西:)
- Ext.define('User', {
- extend: 'Ext.data.Model',
- fields: ['id', 'name', 'email']
- });
- var store = Ext.create('Ext.data.Store', {
- model: 'User',
- proxy: {
- type: 'ajax',
- url : 'users.json',
- reader: {
- type: 'json'
- }
- }
- });
The example above creates a 'User' model. Models are explained in the docs if you're not already familiar with them.(上面例子创建了一个‘User’模型,如果你不熟悉模型,可以产看Model的文档。)
We created the simplest type of JSON Reader possible by simply telling our 's that we want a JSON Reader.(我们创建了一个简单类型的JSON Reader,我们只是简单告诉Store的Proxy我们想要一个JSON Reader。) The Store automatically passes the configured model to the Store, so it is as if we passed this instead:(Store自动传递模型的配置给Store,所以好像我们自己传递的一样。)
- reader: {
- type : 'json',
- model: 'User'
- }
The reader we set up is ready to read data from our server - at the moment it will accept a response like this:(我们建立的reader已经准备好从我们的服务器上读取数据,同时它将接受如下的服务器回应。)
- [
- {
- "id": 1,
- "name": "Ed Spencer",
- "email": "ed@sencha.com"
- },
- {
- "id": 2,
- "name": "Abe Elias",
- "email": "abe@sencha.com"
- }
- ]
Reading other JSON formats
If you already have your JSON format defined and it doesn't look quite like what we have above, you can usually pass JsonReader a couple of configuration options to make it parse your format. (如果你已经有了JSON格式的数据,但是它开起来不像我们上面的,你经常传递给JsonReader一组配置选项让它转换你的格式)For example, we can use the configuration to parse data that comes back like this:(例如我们使用root配置来转换传回来的数据:)
- {
- "users": [
- {
- "id": 1,
- "name": "Ed Spencer",
- "email": "ed@sencha.com"
- },
- {
- "id": 2,
- "name": "Abe Elias",
- "email": "abe@sencha.com"
- }
- ]
- }
To parse this we just pass in a configuration that matches the 'users' above:(为了转换上面的数据,我们传递root配置来匹配‘users’。)
- reader: {
- type: 'json',
- root: 'users'
- }
Sometimes the JSON structure is even more complicated. Document databases like CouchDB often provide metadata around each record inside a nested structure like this:(一些时候JSON结构是非常复杂的。文档数据库像CouchDB 经常提供元素,每个记录都内嵌在结构中,像下面:)
- {
- "total": 122,
- "offset": 0,
- "users": [
- {
- "id": "ed-spencer-1",
- "value": 1,
- "user": {
- "id": 1,
- "name": "Ed Spencer",
- "email": "ed@sencha.com"
- }
- }
- ]
- }
In the case above the record data is nested an additional level inside the "users" array as each "user" item has additional metadata surrounding it ('id' and 'value' in this case). (想上面的例子,记录数据是内嵌在users数据中,每个user项都有附加的元素围绕着它,比如‘id’和‘value’)To parse data out of each "user" item in the JSON above we need to specify the configuration like this:(为了转换每个‘user’项我们需要像下面这样指定记录的配置。)
- reader: {
- type : 'json',
- root : 'users',
- record: 'user'
- }
下面是三个例子:
json.html
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>reader</title>
- <link rel="stylesheet" type="text/css" href="../extjs-4.1.0/resources/css/ext-all.css"/>
- <script type="text/javascript" src="../extjs-4.1.0/bootstrap.js"></script>
- <script type="text/javascript" src="json2.js"></script>
- </head>
- <body>
- </body>
- </html>
------------------------------------------------------------------------------------
json.js
- (function(){
- Ext.onReady(function(){
- Ext.define('User', {
- extend: 'Ext.data.Model',
- fields: ['id', 'name', 'email']
- });
- var store = Ext.create('Ext.data.Store', {
- model: 'User',
- proxy: {
- type: 'ajax',
- url : 'json.jsp',
- reader: {
- type: 'json',
- model: 'User'
- }
- }
- });
- store.load({
- callback: function() {
- //the user that was loaded
- var user = store.first();
- console.log(user);
- console.log("Orders for " + user.get('name') + ":");
- }
- });
- });
- })();
json.jsp
- <%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
- <%
- response.getWriter().write("["
- +"{"
- +"\"id\": 1,"
- +"\"name\": \"Ed Spencer\","
- +"\"email\": \"ed@sencha.com\""
- +" },"
- +"{"
- +" \"id\": 2,"
- +"\"name\": \"Abe Elias\","
- +"\"email\": \"abe@sencha.com\""
- +"}"
- +"]");
- %>
---------------------------------------------------------------------------------
json2.js
- (function(){
- Ext.onReady(function(){
- Ext.define('User', {
- extend: 'Ext.data.Model',
- fields: ['id', 'name', 'email']
- });
- var store = Ext.create('Ext.data.Store', {
- model: 'User',
- proxy: {
- type: 'ajax',
- url : 'json2.jsp',
- reader: {
- type: 'json',
- model: 'User',
- root: 'users'
- }
- }
- });
- store.load({
- callback: function() {
- //the user that was loaded
- var user = store.first();
- console.log(user);
- console.log("Orders for " + user.get('name') + ":");
- }
- });
- });
- })();
json2.jsp
- <%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
- <%
- response.getWriter().write("{"
- +"\"users\": ["
- +"{"
- +" \"id\": 1,"
- +" \"name\": \"Ed Spencer\","
- +" \"email\": \"ed@sencha.com\""
- +" },"
- +" {"
- +" \"id\": 2,"
- +" \"name\": \"Abe Elias\","
- +" \"email\": \"abe@sencha.com\""
- +" }"
- +" ]"
- +"}");
- %>
------------------------------------------------------------------------------------
json3.js
- (function(){
- Ext.onReady(function(){
- Ext.define('User', {
- extend: 'Ext.data.Model',
- fields: ['id', 'name', 'email']
- });
- var store = Ext.create('Ext.data.Store', {
- model: 'User',
- proxy: {
- type: 'ajax',
- url : 'json3.jsp',
- reader: {
- type: 'json',
- model: 'User',
- root : 'users',
- record: 'user'
- }
- }
- });
- store.load({
- callback: function() {
- //the user that was loaded
- var user = store.first();
- console.log(user);
- console.log("Orders for " + user.get('name') + ":");
- }
- });
- });
- })();
json3.jsp
- <%@page language="java" contentType="text/html" pageEncoding="UTF-8"%>
- <%
- response.getWriter().write("{"
- +"\"total\": 122,"
- +"\"offset\": 0,"
- +"\"users\": ["
- +"{"
- +"\"id\": \"ed-spencer-1\","
- +"\"value\": 1,"
- +"\"user\": {"
- +"\"id\": 1,"
- +"\"name\": \"Ed Spencer\","
- +"\"email\": \"ed@sencha.com\""
- +"}"
- +"}"
- +"]"
- +"}");
- %>
1111111111111111
1111111111111