在本机Actionscript3(Flash)中访问解析的JSON(通过php生成)

在本机Actionscript3(Flash)中访问解析的JSON(通过php生成),php,json,actionscript-3,Php,Json,Actionscript 3,好了,开始吧。我肯定我只是错过了一件小事 我正试图为我的游戏设计一个高分系统,因此我在Web空间上有一个php脚本供程序访问(我对php几乎没有经验): 下面是processdata.txt的外观(例如) 然后在我的AC中将其转换为以下内容(以使JSON.parse可读): 现在,如何访问已解析的JSON字符串?我尝试了所有这些,但没有任何效果: 感谢您的帮助。无论您在何处找到实现此功能的可能性(PHP、AC3、JSON等) 另外,如果您有一个简单的可能性让我改变我的php,让它创建xml

好了,开始吧。我肯定我只是错过了一件小事

我正试图为我的游戏设计一个高分系统,因此我在Web空间上有一个php脚本供程序访问(我对php几乎没有经验):

下面是processdata.txt的外观(例如)

然后在我的AC中将其转换为以下内容(以使JSON.parse可读):

现在,如何访问已解析的JSON字符串?我尝试了所有这些,但没有任何效果:

感谢您的帮助。无论您在何处找到实现此功能的可能性(PHP、AC3、JSON等) 另外,如果您有一个简单的可能性让我改变我的php,让它创建xml而不是php,那么我可以从那里做其余的事情,让我的代码处理xml文件


提前感谢。

您应该知道您的JSON内容无效,这就是为什么在ActionScript端无法从中获得任何信息

在保存玩家分数时,可以将数据存储为数组:

[
    {
        "name": "Player1",
        "points": "123"
    }, {
        "name": "Player2",
        "points": "234"
    }
]
但要做到这一点,您必须像这样编辑PHP代码,例如:

<?php

    $json_file = 'scores.json';

    $new_score = $_REQUEST; 

    if(file_exists($json_file))
    {
        // get stored scores
        $scores = json_decode(file_get_contents($json_file));
    } else {
        // create an empty array to store scores
        $scores = array();
    }

    // add the new score to scores array
    array_push($scores, $new_score);    

    // persist scores array as json content
    file_put_contents($json_file, json_encode($scores));

?>

然后在ActionScript端,您可以执行以下操作:

function getProcessdata(e:Event) : void
{
    var rawData:String = e.target.data;

    var data:Array = JSON.parse(rawData) as Array;

    for(var i:int = 0; i < data.length; i++){
        trace(data[i].name);    // gives for example : "Player1"
    }   
}
函数getProcessdata(e:事件):void { var rawData:String=e.target.data; var data:Array=JSON.parse(rawData)作为数组; 对于(变量i:int=0;i
希望这能有所帮助。

您可以在php中创建一个xml文件,而不是.txt。但这会使它成为一个xml文件,还是仍然保存在JSON中?它将另存为.xml文件是的,但它是用正确的xml还是JSON编写的?我该怎么做呢?您的php脚本将创建一个xml,其中包含您发布到脚本中的数据,并保存在服务器上的某个位置,该文件将使用您的新发布数据进行更新,等等。。。您的xml格式是什么?注意,对于少量JSON数据,这是可以的,但对于大量数据,解析可能会在解析过程中使应用程序闲置(由于大型对象/数组实例化)在这种情况下,建议切换到不同的数据格式,例如xml。@BotMaster如何更快地解析大量xml数据?xml的加载是异步的,因此无论应用程序有多大,它都不会使应用程序闲置。在大多数情况下,访问xml数据可以高效地进行,而无需大循环。无需解析XML数据(转换为对象)。但是,解析大型JSON数据可能会使应用程序闲置,因为解析基本上是创建新的对象和数组对象。
"{\"name\":\"Player1\",\"points\":\"123\"}{\"name\":\"Player2\",\"points\":\"234\"}"
proData.name;
proData[name];
proData["name"];
proData[0];
proData[name[0]];
for (var obj : String in proData){
    obj;
}
[
    {
        "name": "Player1",
        "points": "123"
    }, {
        "name": "Player2",
        "points": "234"
    }
]
<?php

    $json_file = 'scores.json';

    $new_score = $_REQUEST; 

    if(file_exists($json_file))
    {
        // get stored scores
        $scores = json_decode(file_get_contents($json_file));
    } else {
        // create an empty array to store scores
        $scores = array();
    }

    // add the new score to scores array
    array_push($scores, $new_score);    

    // persist scores array as json content
    file_put_contents($json_file, json_encode($scores));

?>
function getProcessdata(e:Event) : void
{
    var rawData:String = e.target.data;

    var data:Array = JSON.parse(rawData) as Array;

    for(var i:int = 0; i < data.length; i++){
        trace(data[i].name);    // gives for example : "Player1"
    }   
}