将AJAX HTML响应读入JavaScript数组

Okay, so I have an AJAX function that gets a JSON encoded string from a remote PHP file, the response looks like so..

{"zone_id":"1","zone_name":"Test Zone 1","zone_tilemap":"0,0,0,0*0,0,0,0*0,0,0,0*0,0,0,0","zone_objectmap":"0,0,0,0*0,0,0,0*0,0,0,0*0,0,0,0"}

I won't go too far into what this code is about, but the part I need right now is the tilemap, I need to somehow read those numbers into a mutlidimensional JavaScript array so it looks like so...

var someArray = new Array([0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]);

I know that in PHP there is an explode function that can break the string apart by the asterix and then by the commas and put the results into an array, but I'm not great at JavaScript and have no idea how to accomplish this, any ideas?

My AJAX function so far...

function getLocalZoneInformation(){
    $.ajax({
        type: 'POST',
        url: "./inc/backend/game.functions.php?getLocalZoneInformation=" + localCharacterZoneID,
        success: function(response){
            var localZoneInformation = jQuery.parseJSON(response);

            localZoneID = localZoneInformation.zone_id;
            localZoneName = localZoneInformation.zone_name;
            localZoneTileMap = localZoneInformation.zone_tilemap;
            localZoneObjectMap = localZoneInformation.zone_objectmap;
        }
    });
}
var tmp = localZoneTileMap.split("*");

var someArray = [];
for (i = 0; i < tmp.length; i++) {
    someArray.push(tmp[i].split(","));
}

If using JavaScript 1.6 or newer, you can use the map() method

var someArray = localZoneTileMap.split("*").map(function(tileMap) {
    return tileMap.split(",");
});

Try this:

zones = localZoneName.zone_tilemap.split("*")
out_array = []
for (i in zones) {
    out_array.push(zones[i].split(","))
}

Result will be saved in out_array var.

  • Create a typecast (array)
  • loop your object
  • push each elem to the array