如何将JSON数据从1分配/解析为另一个?

时间:2013-12-05 07:54:39

标签: actionscript-3 flash parsing actionscript

并尝试使用几个字段和搜索按钮创建此搜索表单,单击搜索按钮时,它将转到下一页以显示结果并将结果放入框中。例如。结果1具有(名称,位置,zip),结果2(名称,位置,zip)等

我需要的是,php用于搜索和回显JSON中的数据,以及AS3 VO以JSON格式接收数据,拆分数据并将数据解析为Vector文件,然后将它们放入框中。

我相信我只是错过了最后一步,我可以将JSON引入AS3,我可以跟踪数据。但我无法从第一个AS(searchVO)文件到Vector AS(SEarchVector)文件。 请给我一些意见谢谢。

AS3 VO(接收JSON)

function Asandler(event:Event):void{

 trace( event.target.data );

    var resultString  :String = event.target.data;
    // parse result string as json object
    var resultObject  :Object  = JSON.parse(  resultString );
    // loop all keys in the object
    for( var s:String in resultObject )
    {

        switch(s)
        {
            case 'nobed'
                :'nobed', resultObject[s] ;

                break;

            case 'zip'
                :zip=( 'zip', resultObject[s] );

                break;

            case 'Location'
                :Location=( 'Location', resultObject[s] );

                break;

            case 'price'
                :price=( 'price', resultObject[s] );

                break;

        }}}

SearchVector

public class SearchVectorTest extends MovieClip 
    {

        public function SearchVectorTest() 
        {
            super();

            // This part is the parser, i assume i just have to fix this part and the JSON data will come through, but not sure how

        var test:Vector.<searchVO1> = new Vector.<searchVO1>();

            for (var i:int = 0; i < 10; i++) 
            {
                var tests:searchVO1 = new searchVO1();
                tests.nobed = nobed ;
                tests.zip = zip;
                tests.Location = Location[i];
                tests.price = price[i];
                test.push(tests);
            }



            // using that we can loop through every book in the entry




                for (var j:int = 0; j < test.length; j++) 
            {
                trace(test[j].nobed);
                trace(test[j].zip);
                trace(test[j].Location);
                trace(test[j].price);
                test[j].nobed;
                test[j].zip;
                test[j].Location;
                test[j].price;

            }






            //  loop and put the text into boxes

            var currentY:int = 270;

            for (var k:int = 0; k < test.length; k++) 
            {
                var Bolder:Listing5 = new Listing5();

                Bolder.x=80;

                var bf:TextField = new TextField();
                var bf1:TextField = new TextField();
                var bf2:TextField = new TextField();
                var bf3:TextField = new TextField();

                bf3.width = 100;
                bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);

                bf.width = 100;
                bf.autoSize = TextFieldAutoSize.CENTER;
                bf1.width = 100;
                bf1.autoSize = TextFieldAutoSize.CENTER;
                bf2.autoSize = TextFieldAutoSize.CENTER;
                bf3.autoSize = TextFieldAutoSize.CENTER;
                bf3.width = 100;
                bf1.y= bf.height+5;



                    // Pulling the textfields content out from the current bookVO

                bf.text = test[k].nobed;
                bf1.text = test[k].zip;
                bf2.text = test[k].Location;
                bf3.text = test[k].price;

                bf1.x = (Bolder.height-bf.height)*.5

                bf3.x = (Bolder.height-bf.height)*.5


                bf.x = (Bolder.height-bf.height)*.5
                bf.y = (Bolder.height-bf.height)*.15
                Bolder.addChild(bf);
                Bolder.addChild(bf1);
                Bolder.addChild(bf2);
                Bolder.addChild(bf3);


                    // position the object based on the accumulating variable.
                Bolder.y = currentY;


                //visualAssetForBook.y = (visualAssetForBook.height+15)*k;
                addChild(Bolder);
                currentY += Bolder.height + 35;
            }

        }   
    }

}

刚尝试过,它有2个错误, 在这一行

searchVO文件中的

searchVOs[i] = new searchVO1(resultArray[i].nobed, resultArray[i].zip, resultArray[i].Location, resultArray[i].price );。 错误说Incorrect number of arguments. Expect no more than 0

在它下面1行,createBoxes(searchVOs);它有错误,

Call to a possibly undefined method createBoxes.

1 个答案:

答案 0 :(得分:0)

如果您想将其作为VO传递,最好使用以下解决方案:

PHP:

$result = mysqli_query($con,$sql);

// create your output array
$output = array();

// fetch your results
while( $row = mysqli_fetch_assoc($result) !== false )
{
    // add result row to your output's next index
    $output[] = $row;
}

// echo the json encoded object
echo json_encode( $output ); 

现在您可以直接检索结果,如下所示:

SearchVO.as:

package
{
    public class SearchVO
    {
        public var nobed        :String;
        public var zip          :String;
        public var location     :String;
        public var price        :Number;

        public function SearchVO( nobed:String, zip:String, location:String, price:Number )
        {
            this.nobed      = nobed;
            this.zip        = zip;
            this.location   = location;
            this.price      = price;
        }
    }
}

ASHandler功能:

// retrieve data from php call
var resultString :String = event.target.data;
// parse result string as json object and cast it to array
var resultArray  :Array  = JSON.parse( resultString ) as Array;
// get the length of the result set
var len:int = resultArray.length;

// create vector of SearchVO
var searchVOs:Vector.<SearchVO> = new Vector.<SearchVO>();
// loop the result array
for( var i:int = 0; i<len; ++i )
{
    searchVOs[i] = new SearchVO(resultArray[i].nobed, resultArray[i].zip, resultArray[i].Location, resultArray[i].price );   
}

// call a function to create your boxes
createBoxes(searchVOs);
// or maybe create your SearchVector class and pass it your search vector
var mySearchVector:SearchVector = new SearchVector(searchVOs);

createBoxes函数:

    private function createBoxes( test:vector.<SearchVO> ):void
    {
        //  loop and put the text into boxes

        var currentY:int = 270;

        for( var k:int = 0; k < test.length; k++ ) 
        {
            var Bolder:Listing5 = new Listing5();

            Bolder.x=80;

            var bf:TextField = new TextField();
            var bf1:TextField = new TextField();
            var bf2:TextField = new TextField();
            var bf3:TextField = new TextField();

            bf3.width = 100;
            bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);

            bf.width = 100;
            bf.autoSize = TextFieldAutoSize.CENTER;
            bf1.width = 100;
            bf1.autoSize = TextFieldAutoSize.CENTER;
            bf2.autoSize = TextFieldAutoSize.CENTER;
            bf3.autoSize = TextFieldAutoSize.CENTER;
            bf3.width = 100;
            bf1.y= bf.height+5;



            // Pulling the textfields content out from the current bookVO

            bf.text = test[k].nobed;
            bf1.text = test[k].zip;
            bf2.text = test[k].Location;
            bf3.text = test[k].price;

            bf1.x = (Bolder.height-bf.height)*.5

            bf3.x = (Bolder.height-bf.height)*.5


            bf.x = (Bolder.height-bf.height)*.5
            bf.y = (Bolder.height-bf.height)*.15
            Bolder.addChild(bf);
            Bolder.addChild(bf1);
            Bolder.addChild(bf2);
            Bolder.addChild(bf3);


            // position the object based on the accumulating variable.
            Bolder.y = currentY;


            //visualAssetForBook.y = (visualAssetForBook.height+15)*k;
            addChild(Bolder);
            currentY += Bolder.height + 35;
        }
    }

或SearchVector类:

public class SearchVectorTest extends MovieClip 
{

    public function SearchVectorTest(test:Vector.<SearchVO>) 
    {
        super();

        // This part is the parser, i assume i just have to fix this part and the JSON data will come through, but not sure how

        // REMOVE THAT !
        /*
    var test:Vector.<searchVO1> = new Vector.<searchVO1>();

        for (var i:int = 0; i < 10; i++) 
        {
            var tests:searchVO1 = new searchVO1();
            tests.nobed = nobed ;
            tests.zip = zip;
            tests.Location = Location[i];
            tests.price = price[i];
            test.push(tests);
        }
        */


        // using that we can loop through every book in the entry
        for (var j:int = 0; j < test.length; j++) 
        {
            trace(test[j].nobed);
            trace(test[j].zip);
            trace(test[j].Location);
            trace(test[j].price);
            // trace if you want but calling variable just for fun is not usefull
            /*
            test[j].nobed;
            test[j].zip;
            test[j].Location;
            test[j].price;
            */
        }

        //  loop and put the text into boxes

        var currentY:int = 270;

        for (var k:int = 0; k < test.length; k++) 
        {
            var Bolder:Listing5 = new Listing5();

            Bolder.x=80;

            var bf:TextField = new TextField();
            var bf1:TextField = new TextField();
            var bf2:TextField = new TextField();
            var bf3:TextField = new TextField();

            bf3.width = 100;
            bf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);

            bf.width = 100;
            bf.autoSize = TextFieldAutoSize.CENTER;
            bf1.width = 100;
            bf1.autoSize = TextFieldAutoSize.CENTER;
            bf2.autoSize = TextFieldAutoSize.CENTER;
            bf3.autoSize = TextFieldAutoSize.CENTER;
            bf3.width = 100;
            bf1.y= bf.height+5;

            // Pulling the textfields content out from the current bookVO

            bf.text = test[k].nobed;
            bf1.text = test[k].zip;
            bf2.text = test[k].Location;
            bf3.text = test[k].price;

            bf1.x = (Bolder.height-bf.height)*.5

            bf3.x = (Bolder.height-bf.height)*.5


            bf.x = (Bolder.height-bf.height)*.5
            bf.y = (Bolder.height-bf.height)*.15
            Bolder.addChild(bf);
            Bolder.addChild(bf1);
            Bolder.addChild(bf2);
            Bolder.addChild(bf3);


            // position the object based on the accumulating variable.
            Bolder.y = currentY;


            //visualAssetForBook.y = (visualAssetForBook.height+15)*k;
            addChild(Bolder);
            currentY += Bolder.height + 35;
        }

    }   
}

在所有解决方案中,您必须将数组(或向量)传递给函数,您无法像在此部分代码中那样从任何地方检索它:

        for (var i:int = 0; i < 10; i++) 
        {
            var tests:searchVO1 = new searchVO1();
            tests.nobed = nobed ;  // nobed don't exists here
            tests.zip = zip;       // zip don't exists here
            tests.Location = Location[i]; // Location don't exists here and so Location[i]
            tests.price = price[i]; // pricedon't exists here and so price[i]
            test.push(tests);
        }

我希望这可以帮到你

PS:尝试在单词的开头使用大写字母仅用于类名,并使用小写的开始字母表示变量。对每个人来说都更清楚;)(例如:MyClassName,myVarName)