Recently, I found many friends who searched through the keyword "AS3 json". It can be seen that many of them are interested in this aspect. However, there are only a few articles about AS3 JSON, and the same articles are basically searched on the Internet, including my previous articles, there are no examples. I believe it will make a lot of beginner's friends look confused in the cloud. In fact, I also find it strange that json is actually quite simple. How can no one write a good tutorial. I don't want to talk much about this nonsense. I hope that the reprinted friends can indicate the source. Thank you.
First, you need a JSON class library. We recommend that you download the product external Class Library: as3corelib officially provided by adobe. The use of class libraries is not much said, and everyone should know.
Let's talk about getting data first. In this case, as long as you understand the interaction between AS3 and XML, you can understand JSON, because the two are almost the same. The only thing you need to do is to understand the JSON format. For example, in the JSON example below, just like XML, you can generate this format through any dynamic page, or read it through static or even TXT.
[{"Name": "Hans", "age": "32 "},
{"Name": "John", "age": "12 "},
{"Name": "Zaki", "age": "34 "},
{"Name": "Dr. Cox", "age": "88"}]
Package {
Import com. adobe. serialization. json. JSON;
Import flash. display. Sprite;
Import flash. events. Event;
Import flash.net. URLLoader;
Import flash.net. URLRequest;
Public class getjson extends sprite {
Public Function getjson (){
VaR Loader: urlloader = new urlloader ();
Loader. Load (New URLRequest ("http: // 127.0.0.1/JSON. php"); // The JSON path you want to obtain
Loader. addeventlistener (event. Complete, decodejson );
}
Private function decodejson (EVT: Event): void {
VaR persons: array = JSON. Decode (urlloader (evt.tar get). Data );
// Here, you can use arrays to apply data, which is convenient.
For (VAR I = 0; I <persons. length; I ++ ){
Trace (persons [I]. Name );
}
}
}
}
The code for sending data is as follows:
Package {
Import com. Adobe. serialization. JSON. JSON;
Import flash. display. Sprite;
Import flash. Events. event;
Import flash.net .*;
Public class sendjson extends sprite {
Public Function sendjson (){
VaR arr: array = new array ({"name": "Hans", "Age": "32" },{ "name": "John", "Age ": "12" },{ "name": "Zaki", "Age": "34" },{ "name": "dr. cox "," Age ":" 88 "}); // here is the data to be sent, which can be directly written or generated by other methods, but pay attention to the format.
Sendjson (ARR );
}
Private function sendjson (A: array): void {
VaR jsonstring: String = JSON. encode ();
VaR urlvariables: urlvariables = new urlvariables ();
Urlvariables. JSON = jsonstring;
Var urlRequest: URLRequest = new URLRequest (http: // 127.0.0.1/json. php); // here is the dynamic page for receiving data.
UrlRequest. method = URLRequestMethod. POST;
UrlRequest. data = urlVariables;
// Actually, this is the end. The XML below is only the test result.
Var urlLoader: URLLoader = new URLLoader ();
UrlLoader. addEventListener (Event. COMPLETE, onURLLoaderCompleteEvent );
UrlLoader. load (urlRequest );
}
Private function onURLLoaderCompleteEvent (evt: Event): void {
Var xml: XML = new XML(evt.tar get. data );
Trace (xml );
}
}
}