Форум Flasher.ru

Форум Flasher.ru (http://www.flasher.ru/forum/index.php)
-   ActionScript 1.0/2.0 (http://www.flasher.ru/forum/forumdisplay.php?f=93)
-   -   Подарок лета, JSON for AS2 (http://www.flasher.ru/forum/showthread.php?t=142563)

in4core 24.07.2010 19:13

Подарок лета, JSON for AS2
 
В контакте как известно, можно получать 2 формата, 1 - XML, 2 - JSON
JSON приходит к нам ввиде объекта ( а точнее строки - похожей на объект) а именно,

{ 'a':'value', 'b':'value2' ......} Так, по скольку флеш работать с такими типами данных ( а точнее приводить типы таких данных) не может самостоятельно. Вот вам, товарищи мой класс. Возможно , уже кто то делал подобное, или даже висит на всеобщем обозрении, но я не видел. Держите и пользуйтесь кому надо. and...sorry for my bad english....


Код:

/*
hit JSON standart
class can convert such strings {'operator':'value', 'operatorN':'valueN'}
Created by Alexander Sigankov
PLS, TYPE ON FRAME : "com.JSON.HELP();"  then test a movie 'ctrl+enter'**
*/
class com.JSON extends String {

        private static var count:Number = null;
        private static var def_array:Array = [];
        private static var def_2array:Array = [];
        private static var Obj:Object = new Object();

        public static function convert(s:String, method:String):Object {

                /*
                s - string for convert,
                m - method, 3 types : 1 - 'object' , 2 - 'array', 3 - '2array'
                example of JSON input string 'op1':'value1','op2':'value2','op3':'value3','op4':'value4'
                */

                count = s.length;
                s = s.split("'").join('').split('"').join('').split('{').join('').split('}').join('');

                /* cleared string */

                if (s.indexOf(',') == -1 && s.indexOf(':') != -1) {
                        if (method == 'array') {
                                return [s.substr(0, s.indexOf(':')), s.substr(s.indexOf(':')+1, s.length)];
                        } else if (method == 'object') {
                                return Obj[s.substr(0, s.indexOf(':'))]=s.substr(s.indexOf(':')+1, s.length);
                        } else if (method == '2array') {
                                return [[s.substr(0, s.indexOf(':')), s.substr(s.indexOf(':')+1, s.length)]];
                        } else {
                                trace('no method detected, please use a HELP function for true condition');
                                return;
                        }
                } else {
                        if (s.indexOf(',') == -1 && s.indexOf(':') == -1) {
                                trace('scholota detected');
                                return 'error 300. string not coverted : '+s;
                        }
                }
                /* retrive methods :
                array
                2array
                object
                */
                switch (method) {
                        case 'array' :
                                for (var i = 0; i<count; i++) {
                                        def_array.push(s.substr(0, s.indexOf(':')));
                                        def_array.push(s.substr(s.indexOf(':')+1, s.indexOf(',')-s.indexOf(':')-1));
                                        s = s.substr(s.indexOf(',')+1, s.length);
                                        if (s.indexOf(',') == -1) {
                                                def_array.push(s.substr(0, s.indexOf(':')));
                                                def_array.push(s.substr(s.indexOf(':')+1, s.length));
                                                return def_array;
                                        }
                                }
                                break;
                        case 'object' :
                                for (var i = 0; i<count; i++) {
                                        Obj[s.substr(0, s.indexOf(':'))] = s.substr(s.indexOf(':')+1, s.indexOf(',')-s.indexOf(':')-1);
                                        s = s.substr(s.indexOf(',')+1, s.length);
                                        if (s.indexOf(',') == -1) {
                                                Obj[s.substr(0, s.indexOf(':'))] = s.substr(s.indexOf(':')+1, s.length);
                                                return Obj;
                                        }
                                }
                                break;
                        case '2array' :
                                for (var i = 0; i<count; i++) {
                                        def_2array.push([s.substr(0, s.indexOf(':')), s.substr(s.indexOf(':')+1, s.indexOf(',')-s.indexOf(':')-1)]);
                                        s = s.substr(s.indexOf(',')+1, s.length);
                                        if (s.indexOf(',') == -1) {
                                                def_2array.push([s.substr(0, s.indexOf(':')), s.substr(s.indexOf(':')+1, s.length)]);
                                                return def_2array;
                                        }
                                }
                                break;
                        default :
                                return ['no method detected, please use a HELP function for true condition'];
                                break;
                }


                return ['unkown error'];
        }
        public static function parseAndClear(s:String, a:Array, j:Array):String {
                count = s.length;
                for (var i = 0; i<count; i++) {
                        if (a[i] != undefined && j[i] != undefined) {
                                s = s.split(a[i]).join(j[i]);
                                if (a[i+1] == undefined) {
                                        return s;
                                }
                        } else {
                                return 'pls type true split array and join array';
                        }
                }
        }
        public static function watch(s:String):String {
                trace("Watched string is : "+s);
                return s;
        }
        static function HELP(tf:TextField):Void {
                var str:String = "Welcome to JSON class helper.\nCreated by Alex Sigankov(c:tambov).\nHELP : \n";
                str += "Work with class functions : \n";
                str += "Open a new flash AS2 document, then place class folder, near where is u fla document was created, open Action panel on frame, and type :\n";
                str += "trace(com.JSON.function). For example : var str = 'flash : cool'; trace(com.JSON.convert(str,'2array')) // OUTPUT - [[flash,cool]].\n\n"
                str += "Function 'convert(string,method)' have 2 parametrs. 1st is a converted string, 2nd is a method of convert.\n";
                str += "There are 3 methods, such as : 1 - 'array', 2-'object', 3-'2array'";
                str += "\nFunction 'parseAndClear(string,split_array,join_array)' will delete and replace a new symbols in entered string";
                str += "\nExample : str = '4*5&7'; trace(com.JSON.parseAndClear(str,['*'],['&'])) //output : 4&5&7\n";
                str += "Function watch(string) will return input string.\n";
                str += "\nThx for using my class. good luck";
                if (tf) {
                        tf.text = str;
                } else {
                        trace(str);
                }
        }
        private static var version = 1.230710;
}


Хемуль 25.07.2010 15:11

Так Google находит по "as2 json" очень быстро:
http://www.json.org/json.as
Ну и т.д.

in4core 25.07.2010 15:19

Заценил, прикольно. Там геморно слишком у меня удобнее написано)))


Часовой пояс GMT +4, время: 18:12.

Copyright © 1999-2008 Flasher.ru. All rights reserved.
Работает на vBulletin®. Copyright ©2000 - 2026, Jelsoft Enterprises Ltd. Перевод: zCarot
Администрация сайта не несёт ответственности за любую предоставленную посетителями информацию. Подробнее см. Правила.