Javascript Get webpage contents into Array
Alright, I've got a very unique API for a service I run, in which the return values are displayed similarly to JSON (but not JSON), and it works very well with most languages, but now I face a problem accessing the returned values using Javascript only.
I have a PHP page which takes GET parameters, and then displays output like this:
stapi=0,0,0,0,0,0,0,0&stlinks=Hu3L|http://www.example.com,3Ozq|https://www.example.com
That is the only output of the webpage, and what I'd like to do is collect these two variables into a Javascript Array to be displayed as a list. (The second variable I typically collect into matched pairs as you can see the separation with the "|
")
Now I just need to know how to collect the "variables" from the output and put them into an array (these two pages are NOT going to be on the same server) ...
**I want to put the stapi comma-separated values into its own array and then the stlinks vertical-bar matched pairs/comma separated values into its ow开发者_运维知识库n array
Try this function to split your string with this pipe:
function splitString(s,ch){
var n = [];
var i = 0, j = 0;
while (i != -1){
j = i;
i = s.indexOf(ch,i+1);
if (i > -1){
if (j > 0){
n.push( s.substr(j+ch.length,i-j-ch.length) );
} else {
n.push( s.substr(j,i-j) );
}
} else {
n.push( s.substr(j+ch.length) );
}
}
return n;
}
var result = [];
var pairs = splitString("stapi=0,0,0,0,0,0,0,0&stlinks=Hu3L|http://www.example.com,3Ozq|https://www.example.com","|");
var i = 0;
for (i = 0; pairs.length < i; i++){
var temp = splitString(pairs[i],'=');
result.push({name: temp[0], value: temp[1]});
}
What You need is packed to result variable.
精彩评论