Jsonreader configuration in ext

Source: Internet
Author: User

Jsonreader

In JavaScript, JSON is a very important data format. The form of key: value is easier to understand than the complicated tag structure of XML, and the amount of code is smaller, many people tend to use it as the data exchange format of Ext. The code below shows the JSON data prepared for JSON-reader.

var data = {id:0,totalProperty:2,successProperty:true,root:[{id:'id1',name:'name1',descn:'descn1'},{id:'id2',name:'name2',descn:'descn2'}]};

Compared with arrays, JSON supports pagination. The totalproperty parameter can be used to represent the total amount of data. The successproperty parameter is optional. You can use it to determine whether the current request is successfully executed and whether data is loaded. If you do not want jsonreader to process the response data, you can set successproperty to false.

Now let's take a look at jsonreader to see how it corresponds to the above JSON data, as shown in the following code.

var reader = new Ext.data.JsonReader({successProperty: "successproperty",totalProperty: "totalProperty",root: "root",id: "id"}, [{name:'id',mapping:'id'},{name:'name',mapping:'name'},{name:'descn',mapping:'descn'}]);

In the preceding example, the ing method is not concise, because the content of name and mapping is the same. In fact, mapping can be omitted here. By default, the name parameter is used to obtain the corresponding data from JSON. If you do not want to use the same name as in JSON, you can use mapping to modify the name. However, mapping has other purposes here, as shown in 10-3 of the code list.

Code List 10-3 configure mapping for jsonreader

var data = {id:0,totalProperty:2,successProperty:true,root:[{id:'id1',name:'name1',descn:'descn1',person:{id:1,name:'man',sex:'male'}},{id:'id2',name:'name2',descn:'descn2',person:{id:2,name:'woman',sex:'female'}}]};var reader = new Ext.data.JsonReader({successProperty: "successproperty",totalProperty: "totalProperty",root: "root",id: "id"}, ['id','name','descn',{name:'person_name',mapping:'person.name'},{name:'person_sex',mapping:'person.sex'}]);

In the code above, we use JSON to support more complex nested structures. The person object itself has attributes such as ID, name, and sex. In jsonreader, you can use mapping to map these nested internal attributes to the corresponding record, while other fields remain unchanged.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.