代码之家  ›  专栏  ›  技术社区  ›  Marty

Javascript拆分数组并将值分配给NZBMatrix API中的变量

  •  0
  • Marty  · 技术社区  · 15 年前

    简而言之,我要做的是构建一个AdobeAIR应用程序, 使用JavaScript、AJAX通过搜索查询连接到API,这一切都很好。

    当我收到API返回的带有5个结果的“request.responseText”时

    NZBID:444027;
    NZBNAME:test result 1;
    LINK:nzbmatrix.com/nzb-details.php?id=444027&hit=1;
    SIZE:1469988208.64;
    INDEX_DATE:2009-02-14 09:08:55;
    USENET_DATE:2009-02-12 2:48:47;
    CATEGORY:TV > Divx/Xvid;
    GROUP:alt.binaries.test;
    COMMENTS:0;
    HITS:174;
    NFO:yes;
    REGION:0;
    |
    NZBID:444028;
    NZBNAME:another test;
    LINK:nzbmatrix.com/nzb-details.php?id=444028&hit=1;
    SIZE:1469988208.64; = Size in bytes
    

    第一个数组应该使用| 将这5个结果分配给一个新数组。

    第二个数组应使用以下方法拆分每个值: ie:var nzbidtxt=array1[0];我想:

    第三个数组应该从中拆分每个变量; 将这12个值指定给新创建的数组 ie:var-nzbidValue=array2[0];我想: document.write(nzbValue);//ie:打印“444027”

    因此,使用这两个数组,我可以显示返回的帖子列表。。

    nzbid:444027//这将用于直接下载 nzbName:nzb的名称

    我一直致力于的功能如下:

    function breakNzbUrlResponse(text)
    {
        var place = new Array;
        var place2 =new Array;
        var place3 =new Array;
    
        place[0] = text.indexOf('|');
        place2[0] = text.indexOf(':');
        place3[0] = text.indexOf(';');
    
        var i = 1;
        while(place[i-1] > 0 || i==1) {
            place[i] = text.indexOf('|',place[i-1]+1);
            place2[i] = text.indexOf(':',place2[i-1]+1);
            if(place2[i] == -1)
                {
                place2[i] = text.length;
            }
            i++;
        }
        i=1;
        var vars = new Array;
        var values = new Array;
        var retarray = new Array;
        vars[0] = text.substr(0,place[0]);
        values[0] = text.substr((place[0]+1),((place2[0]-place[0])-1));
        retarray[vars[0]] = values[0];
        while(i < (place.length-1) || i==1)
            {
            vars[i] = text.substr((place2[i-1]+1),((place[i]-place2[i-1])-1));
            values[i] = text.substr((place[i]+1),((place2[i]-place[i])-1));
            //alert('in loop\r\nvars['+i+'] is: '+vars[i]+'\r\nvalues['+i+'] is: '+values[i]);
            retarray[vars[i]] = values[i];
            i++;
        }
        return retarray;
    }
    

    对于这种类型来说,这感觉和看起来都是一个非常冗长的过程。。 我想做的就是给每个返回类型分配一个新变量 即

     var nzbid = array3[0];
    

    拆分时将引用返回字符串的第一行NZBID:444027;其中NZBID的价值为44027。。

    谢谢 马蒂

    2 回复  |  直到 15 年前
        1
  •  1
  •   Mike Buckbee    15 年前

    逻辑是:

    ResultsArray = split by "|"  
    FieldArray = Each element of FieldArray split by ";"  
    ValueArray = Each element of FieldArray split by ":"  
    
        2
  •  0
  •   Error 454    13 年前

    //used to hold temporary key/value pairs
    var tempKV = {};
    
    //used to hold the search results
    this.searchResults = [];
    
    //The unformatted search results arrive in inResponse
    //Remove whitespace and newlines from the input
    inResponse = inResponse.replace(/(\r\n|\n|\r)/gm,"");
    
    //search entries are delimited by |
    var results = inResponse.split("|");
    for(var i = 0; i < results.length; i++){
        //key:value pairs in each search result are dlimited by ;
        var pair = results[i].split(";");    
    
        for(var j = 0; j < pair.length; j++){
            //keys and values are delimited by :
            var kv = pair[j].split(":");
    
            //normal key:value pairs have a length of 2
            if(kv.length == 2){
                //make sure these are treated as strings
                //tempKV["key"] = "value"
                tempKV["" + kv[0]] = "" + kv[1];
            }
            //Else we are parsing an entry like "http://" where there are multiple :'s
            else if(kv.length > 2){
                //store the first chunk of the value
                var val = "" + kv[1]; 
    
                //loop through remaining chunks of the value
                for(var z = 2; z < kv.length; z++){
                    //append ':' plus the next value chunk
                    val += ":" + kv[z]; 
                }
                //store the key and the constructed value
                tempKV["" + kv[0]] = val; 
            }
        }
        //add the final tempKV array to the searchResults object so long
        //as it seems to be valid and has the NZBNAME field
        if(tempKV.NZBNAME){
            this.searchResults[i] = tempKV;
        }
        //reset the temporary key:value array
        tempKV = {};
    }
    
    //all done, this.searchResults contains the json search results