dongwei5740 2012-03-06 03:09
浏览 44
已采纳

将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;
        }
    });
}
  • 写回答

3条回答 默认 最新

  • dongtong5242 2012-03-06 03:12
    关注
    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(",");
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?