[Original] basics: Article 4. There are two important methods in ext: decode and encode.
Before proceeding to this example, I suppose you already know what JSON data is, so let's review it here:
JSON (JavaScript Object Notation) is a data exchange format, using a completely language-independent text format;
JSON is constructed in two structures: an ordered list of the set and values of the "name/value" pair
The following details:
A collection of name/value pairs ). In different languages, it is understood as an object, record, struct, dictionary, and hash table ), keyed list or associative array ).
An ordered list of values ). In most languages, it is understood as an array ).
JSON has the following forms:
An object is an unordered set of 'name/value' pairs. An object starts with "{" (left parenthesis) and ends with "}" (right Parenthesis. Each "name" is followed by a ":" (colon); "," (comma) is used to separate the "name/value" pairs.
An array is an ordered set of values. An array starts with "[" (left square brackets) and ends with "]" (right square brackets. Values are separated by commas.
Value can be a string, number, true, false, null, object, or array enclosed by double quotation marks ). These structures can be nested.
A string is a collection of any number of Unicode characters enclosed by double quotes. It is escaped using a backslash. A character (character) is a separate string (character string ).
Blank space can be added to any symbol
------------------------ The following is the latest report from Baidu.
Next we will introduce two very important methods in Ext. In fact, there are no redundant methods in ext, and each method can play its role properly. Here it is very important, because they are too common, especially when exchanging data with databases.
Ext has two very important methods: Decode, encode, encoding, and decoding. Do you really think so?
Strictly speaking, one is to convert a JSON string into an object, and the other is to convert an object into a JSON string.
The following example mainly describes Ext. decode () and ext. encode () usage, where strings in JSON format are used. First, ext. the decode () method converts a JSON string to an object, and then displays the values of each attribute contained in the object through the message box. Then, ext is used. encode () converts generated objects into strings in JSON format that are defined at the beginning. It also displays them in the message box and adds a function, in this case, the first message box pops up three seconds before the second message box pops up.
Use the image description.
// The following is a message box generated by the object access attribute after the JSON string is converted to an object.
// The following is the message box generated after the above object is converted into a JSON string
Let's take a look at the example below.
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "testdecode. aspx. cs" inherits = "test. example. Hello. testdecode" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> test Ext. Decode () and Ext. encode () methods. </title>
<LINK rel = "stylesheet" type = "text/CSS" href = "http://www.cnblogs.com/ExtJS/resources/css/ext-all.css"/>
<LINK rel = "stylesheet" type = "text/CSS" href = "http://www.cnblogs.com/ExtJS/resources/css/xtheme-purple.css"/>
<SCRIPT type = "text/JavaScript" src = "http://www.cnblogs.com/ExtJS/adapter/ext/ext-base.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "http://www.cnblogs.com/ExtJS/ext-all.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "http://www.cnblogs.com/ExtJS/ext-lang-zh_CN.js"> </SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<SCRIPT type = "text/JavaScript">
Function ready ()
{
// Use another example to illustrate how to use decode and encode. Now we know what the JSON data format is and what its functions and syntax are.
// Now let's start the drill.
// Use the decode method to convert a JSON string to an object.
// Step 1: Define a JSON string first.
VaR Liu Tianwang = "{Name: 'andy Lau ', Gender: 'male', hometown: 'Hong Kong '}";
// Step 2: now we want to convert Liu Tianwang into an object.
VaR who = ext. Decode (Liu Tianwang );
// Step 3: After who is an object, it is equivalent to a class object. The name, gender, and hometown are all attributes of WHO. Now you know how to access the attributes through the object.
VaR name = who. Name; // obtain the [name] attribute of the WHO object
VaR sex = who. Gender; // obtain the [gender] attribute of the WHO object
VaR home = who. Home; // obtain the [home] attribute of the WHO object
// Step 4: Combine the obtained information
VaR result = "Liu Tianwang information: name is --" + name + "; gender --" + sex + "; hometown --" + home;
// Step 5: display the information of the obtained who object in the pop-up message.
Ext. msg. Alert ("information about Liu Tianwang", result );
// The preceding example shows how to access the attributes of an object one by one after converting a JSON string into an object.
// Use the encode method below
// The encode method is simple, that is, convert the generated who object to the JSON string defined in step 1.
// Define a time-based execution function
VaR getjson = function ()
{
// You can write this section out of the function. The reason why this section is included is to display the following message window three seconds after the first message box is displayed.
VaR jsonstr = ext. encode (WHO );
// We can display the JSON string through the pop-up message.
Ext. msg. Alert ("jsonstr information content", jsonstr );
};
// The following method indicates that after 3 seconds, the getjson function will be called to execute the script contained in it.
SetTimeout (FIG, 3000 );
// Add something
// Ext. Decode () and Ext. encode () are short for Ext. util. JSON. Decode () and Ext. util. JSON. encode respectively.
}
Ext. onready (ready );
</SCRIPT>
</Div>
</Form>
</Body>
</Html>
Now, this example shows the perfect effect of Ext. Decode () and Ext. encode () methods.
Use the image description.
// The following is a message box generated by the object access attribute after the JSON string is converted to an object.
// The following is the message box generated after the above object is converted into a JSON string