ExtJS in Decode and encode (reprint)

Source: Internet
Author: User
Tags access properties

Originating From: http://blog.163.com/xiao_mege/blog/static/72942753201102693545195/

Before I tell you about this example, I assume you already know what JSON data is, so here's a refresher:

JSON (JavaScript Object Notation) is a data interchange format that uses completely language-independent text formats;
JSON is constructed in two structures: a set of name/value pairs and an ordered list of values

The following details are explained below:
A collection of name/value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative Array).
The sequence of values (an ordered list of values). In most languages, it is understood as an array (array).
The JSON has these forms:

An object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" is followed by a ":" (colon); "' Name/value ' pair ' is separated by", "(comma).
An array is an ordered collection of values (value). An array begins with "[" (the left square bracket), and "]" (the right square bracket) ends. Use "," (comma) to separate values.

The value (value) can be a string enclosed in double quotation marks (string), a numeric value (number), ture, false, NULL, an object, or an array. These structures can be nested.

A string is a collection of any number of Unicode characters surrounded by double quotation marks, escaped with a backslash. A character (character) is a separate string (character string).
Whitespace can be added to any symbol

------------------------The following latest reports from Baidu

Here are two important methods in Ext, in fact, there is no redundant method in Ext, each method can play its role properly, this refers to it is very important, because they are too common, especially when exchanging data with the database
There are two important methods in Ext, one is decode and the other is encode. As the name implies, one is code, one is decoding, do you really think so?
Strictly speaking, one is to convert a JSON string into an object, and one is to convert the object to a JSON string

The following example focuses on the use of Ext.decode () and Ext.encode (), where a JSON-formatted string is used, first using the Ext.decode () method to convert a JSON-formatted string into an object. The object is then accessed by accessing the values of each property contained in the object, displayed through a message box, and then using Ext.encode () to convert the generated object into a JSON-formatted string that starts with a message box, adding a function, Causes the second message box to pop up after 3 seconds of the first message box

Use the picture to illustrate it.

///The following is a message box generated from an object access property after converting a JSON string to an object

//The following is a message box generated after converting the object generated above into a JSON string

Take a look at the example.

<%@ 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">
http://www.w3.org/1999/xhtml">
<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>
<body>
<form id= "Form1" runat= "Server" >
<div>
<script type= "Text/javascript" >

function Ready ()
{
Another example illustrates how to use decode and encode, and now we know what the JSON data format is, and what it does and what it does.
Now let's get to the drill.
Use the Decode method first, which is to convert the JSON string into an object's

First step: Define a JSON string first.
var Liu King = "{name: ' Andy Lau ', Sex: ' Male ', Hometown: ' Hong Kong '}";

Step two: Now we're going to convert the king to a target.
var who = Ext.decode (Liu Tianwang);

The third step: who became the object after the equivalent is the object of the class, the name, gender, home has become the WHO properties, now know how to access properties through the object bar
var name = who. Name; Get the [name] property of the Who object
var sex = who. gender; Get the [gender] property of the Who object
var home = who. Home; Get the [home] property of the Who object

Fourth step: Combine the information obtained below
var result = "The information of the Liu King's information: name is-" +name + "; gender-" +sex+ "; hometown-" +home;

The fifth step: we show the information of the WHO object we get with the pop-up message.
Ext.Msg.alert ("Liu King's information", result);

The above is an example of converting a JSON string into an object and then accessing the properties of the object individually

Use the Encode method below
The Encode method is simple enough to convert the WHO object generated above into the JSON string defined in the first step

Define a function to time execution
var Getjson = function ()
{
You can completely write this in the outside of the function, the reason is that it is purely to display the following message window 3 seconds after the first message box is displayed

var jsonstr = Ext.encode (WHO);

Let's just show this JSON string in the form of a popup message.
Ext.Msg.alert ("Jsonstr information content", JSONSTR);
};

The following method means: After 3 seconds, the function Getjson executes the script contained inside.

SetTimeout (getjson,3000);

Add something.

Ext.decode () and Ext.encode () are shorthand for Ext.util.JSON.decode () and Ext.util.JSON.encode, respectively

}
Ext.onready (Ready);
</script>
</div>
</form>
</body>

Now This example illustrates the perfect effect of the Ext.decode () and Ext.encode () methods.

Use the picture to illustrate it.

///The following is a message box generated from an object access property after converting a JSON string to an object

//The following is a message box generated after converting the object generated above into a JSON string

ExtJS in Decode and encode (reprint)

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.