开发者

Javascript Split Array and assign values to variables from NZBMatrix API

开发者 https://www.devze.com 2022-12-10 01:16 出处:网络
Not sure if any of you guys/girls out there that uses the NZBMatrix website API.. In short what I\'m trying to do is build an Adobe Air Application,

Not sure if any of you guys/girls out there that uses the NZBMatrix website API..

In short what I'm trying to do is build an Adobe Air Application, using JavaScript, AJAX to connect to the API with a search query, this is all good.

When i receive the "request.responseText" back from the API with the 5 results (can only be 5) I'm having trouble with the JavaScript split function trying to split them all out...

the return string is returned as follows:

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

etc..etc..

the first Array should split each set of results using | assign those 5 results to a new array.

the 2nd Array should split each value using : assign those 12 results to new variables ie: var nzbidtxt = array1[0]; which would echo like: document.write(nzbidtxt); // ie: print "NZBID:"

the 3rd Array should split each variable from ; assign those 12 values to the newly created array ie: var nzbidValue = array2[0]; which would echo like: document.write(nzbValue); // ie: print "444027"

so using both arrays I can display a listing of the posts returned.. in a nice usable format..

nzbid: 444027 // this will be used for direct download nzbName: the name of the nzb etc..etc..

the function i have been working on is below:

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

This feels and looks like a very long winded process for this type.. all I want to do is basically assign a new variable to each return type ie

 var nzbid = array3[0];

which when split would reference the first line of the return string, NZBID:444027; where the value for NZBID would be 44027..

bit of a book going on, but the more info the better i suppose. Thanks Marty


You could probably cut out a significant number of lines of code by further utilizing split() instead of the manual dissections of the entries and using multidimensional arrays instead of repeatedly creating new arrays.

The logic would be:

ResultsArray = split by "|"  
FieldArray = Each element of FieldArray split by ";"  
ValueArray = Each element of FieldArray split by ":"  


2 years later, it's sad that NZBMatrix is still using this horrible format. Here is how you can parse it.

//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
0

精彩评论

暂无评论...
验证码 换一张
取 消