不确定是否有人使用NZBMatrix网站API。
简而言之,我要做的是构建Adobe Air应用程序, 使用JavaScript,AJAX通过搜索查询连接到API,这一切都很好。
当我从API收到带有5个结果的“request.responseText”时 (只能是5)我在使用JavaScript分割函数时遇到了麻烦,试图将它们全部分开......
返回字符串返回如下:
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 ..
第一个数组应使用|分割每组结果 将这5个结果分配给一个新数组。
第二个数组应使用以下方法拆分每个值: 将这12个结果分配给新变量 即:var nzbidtxt = array1 [0];这会像: 文件撰写(nzbidtxt); //即:打印“NZBID:”
第3个数组应该分割每个变量; 将这12个值分配给新创建的数组 即:var nzbidValue = array2 [0];这会像: 文件撰写(nzbValue); //即:打印“444027”
所以使用两个数组我可以显示返回的帖子的列表.. 以一种很好的可用格式..
nzbid:444027 //这将用于直接下载 nzbName:nzb的名称 etc..etc ..
我一直在努力的功能如下:
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 ..
有一本书正在进行中,但我认为越多的信息就越好。 谢谢 玛蒂答案 0 :(得分:1)
您可以通过进一步利用split()而不是手动剖析条目并使用多维数组而不是重复创建新数组来删除大量代码行。
逻辑是:
ResultsArray = split by "|"
FieldArray = Each element of FieldArray split by ";"
ValueArray = Each element of FieldArray split by ":"
答案 1 :(得分:0)
2年后,令人遗憾的是,NZBMatrix仍在使用这种可怕的格式。以下是解析它的方法。
//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