Ext.encode (Mixed O): String:json object Conversion JSON string Ext.decode (String json): Object:json string Conversion JSON object
Instance
HTML code
- <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>
Ext.encode and Ext.decode.