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条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算