1

Okay, here goes. I'm sure I'm only missing a small thing somewhere.

I'm trying to do a little Highscore System for my game and thus I have a php script on a webspace for the programm to access (I have literally 0 experience with php):

<?php
    $json = json_encode($_REQUEST);
    $fileSave = fopen("processdata.txt", "a+") or die ("Can't create processdata.txt");
    fwrite($fileSave, $json);
    fclose($fileSave);
    print($json);
?>
processdata.php

then, in actionscript, I juse this code to access the file and parse it:

//set variables to send to the php script
var variables:URLVariables = new URLVariables();
variables["name"] = "Player1";
variables["points"] = 123;

//set request to load php script
var request:URLRequest = new URLRequest("processdata.php");
request.method = URLRequestMethod.POST;
request.data  = variables;

var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, httpRequestComplete);
loader.load(request);

//php script finished
function httpRequestComplete(_e:Event){
    //load processdata.txt
    var uLoader:URLLoader = new URLLoader();
    var uReq:URLRequest = new URLRequest("processdata.txt");
    uLoader.addEventListener(Event.COMPLETE, getProcessdata);
    uLoader.load(uReq);
}

//loading of processdata.txt finished
function getProcessdata(_e:Event){
    var rawData : String = _e.target.data;
    //convert the input string in readable JSON
    rawData = rawData.split('"').join("\\\"");
    rawData = "\"" + rawData + "\"";
    //convert into JSON
    var proData:Object = JSON.parse(rawData);
}

here is what processdata.txt looks like (for example)

{"name":"Player1","points":"123"}{"name":"Player2","points":"234"}

this is then converted in my AC into this (to make it readable for the JSON.parse):

"{\"name\":\"Player1\",\"points\":\"123\"}{\"name\":\"Player2\",\"points\":\"234\"}"

Now, how to I access the parsed JSON String? I tried all of those and nothing works:

proData.name;
proData[name];
proData["name"];
proData[0];
proData[name[0]];
for (var obj : String in proData){
    obj;
}

Any help is appreciated. Doesn't matter where you find a possibility to make this work (PHP, AC3, JSON, etc) Also, if you have a simple possibility for me to change my php in a way that it creates xml instead of php, I can do the rest from there, I get my Code to work with a XML File.

Thanks in advance.

5
  • you can create a xml file instead .txt in php. Commented Jan 18, 2016 at 16:51
  • but does that make it a xml file or does it still save it in JSON? Commented Jan 18, 2016 at 20:01
  • it will save as .xml file Commented Jan 18, 2016 at 20:11
  • Yes, but will it be written in correct xml or in JSON? And how would I do that? Commented Jan 19, 2016 at 9:41
  • your php script will create a xml with data you post to the script and save at some location on server that file will update with your new post data and so on... what will be your xml format? Commented Jan 19, 2016 at 10:09

1 Answer 1

2

You should know that your JSON content is not valid and that's why you can get nothing from it in the ActionScript side.

As you are saving players scores, you can store your data as an array :

[
    {
        "name": "Player1",
        "points": "123"
    }, {
        "name": "Player2",
        "points": "234"
    }
]

but to get that, you've to edit your PHP code like this, for example :

<?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));

?>

then in the ActionScript side, you can do :

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"
    }   
}

Hope that can help.

Sign up to request clarification or add additional context in comments.

3 Comments

note for small amount of JSON data this is fine but for large amount of data the parsing could idle the application during the parsing (due to large Object/Array instantiation) in that case it is recommended to switch to a different data format like xml for example.
@BotMaster how would parsing a large amount of xml data be faster?
the loading of xml is asynchronous so it will not idle the app no matter how big it is. The accessing the xml data can be done efficiently without large loops most of the time. There's no need to parse XML data (translate into objects). The parsing of large JSON data however could idle the app since parsing is basically creating new Object and Array objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.