简体   繁体   English

将url编码的数据转换为JSON数组或js数组

[英]convert url encoded data to JSON array or js array

How can I convert data that looks like it is url encoded to a JSON array or js array. 如何将看起来像是url编码的数据转换为JSON数组或js数组。

&nfl_s_delay=120&nfl_s_stamp=1109033755&nfl_s_left1=Cleveland%2024%20%20%20Cincinnati%203%20(END%20OF%204TH)&nfl_s_right1_count=0&nfl_s_url1=http://sports.espn.go.com/nfl/boxscore?gameId=400554328&nfl_s_left2=Kansas%20City%2017%20%20%20Buffalo%2013%20(00:00%20IN%204TH)&nfl_s_right2_count=0&nfl_s_url2=http://sports.espn.go.com/nfl/boxscore?gameId=400554332&nfl_s_left3=Miami%2016%20%20%20Detroit%2020%20(00:00%20IN%204TH)&nfl_s_right3_count=0&nfl_s_url3=http://sports.espn.go.com/nfl/boxscore?gameId=400554355&nfl_s_left4=^Dallas%2031%20%20%20Jacksonville%2017%20(FINAL)&nfl_s_right4_count=0&nfl_s_url4=http://sports.espn.go.com/nfl/boxscore?gameId=400554358&nfl_s_left5=San%20Francisco%2027%20%20%20New%20Orleans%2024%20(END%20OF%201ST%20OT)&nfl_s_right5_count=0&nfl_s_url5=http://sports.espn.go.com/nfl/boxscore?gameId=400554362&nfl_s_left6=Tennessee%207%20%20%20Baltimore%2021%20(00:00%20IN%204TH)&nfl_s_right6_count=0&nfl_s_url6=http://sports.espn.go.com/nfl/boxscore?gameId=400554367&nfl_s_left7=Pittsburgh%2013%20%20%20NY%20Jets%2020%20(END%20OF%204TH)&nfl_s_right7_count=0&nfl_s_url7=http://sports.espn.go.com/nfl/boxscore?gameId=400554370&nfl_s_left8=Atlanta%2027%20%20%20Tampa%20Bay%2017%20(END%20OF%204TH)&nfl_s_right8_count=0&nfl_s_url8=http://sports.espn.go.com/nfl/boxscore?gameId=400554372&nfl_s_left9=Denver%2041%20%20%20Oakland%2010%20(00:00%20IN%203RD)&nfl_s_right9_count=0&nfl_s_url9=http://sports.espn.go.com/nfl/boxscore?gameId=400554396&nfl_s_left10=St.%20Louis%2014%20%20%20Arizona%2010%20(00:00%20IN%202ND)&nfl_s_right10_count=0&nfl_s_url10=http://sports.espn.go.com/nfl/boxscore?gameId=400554397&nfl_s_left11=NY%20Giants%2017%20%20%20Seattle%2017%20(00:00%20IN%203RD)&nfl_s_right11_count=0&nfl_s_url11=http://sports.espn.go.com/nfl/boxscore?gameId=400554400&nfl_s_left12=Chicago%20at%20Green%20Bay%20(8:30%20PM%20ET)&nfl_s_right12_count=0&nfl_s_url12=http://sports.espn.go.com/nfl/preview?gameId=400554403&nfl_s_left13=Carolina%20at%20Philadelphia%20(8:30%20PM%20ET)&nfl_s_right13_count=0&nfl_s_url13=http://sports.espn.go.com/nfl/preview?gameId=400554408&nfl_s_count=13&nfl_s_loaded=true

I would split split by &nfl_s_left2= , but the parameter increases by one for each time. 我会用&nfl_s_left2 =拆分,但是参数每次都会增加一。 So I could split by &nfl_s_left2= but that would only split once as the next thing that is similar is &nfl_s_left3= . 所以我可以除以&nfl_s_left2 =,但是只能分割一次,因为下一个类似的是&nfl_s_left3 =。 So I guess a more specific question is can I split by a variable string? 因此,我想一个更具体的问题是我可以用可变字符串分割吗? I could split by &nfl_s_left but then I'd get the number= in the value of the array. 我可以用&nfl_s_left拆分,但是然后我将在数组的值中获取number =。

What would be the best way I could clean up this data? 清除这些数据的最佳方法是什么?

how about this 这个怎么样

function url_split(str){
    var jsArry={},tmp = str.split('&nfl_s_'),tmp2;
    tmp.forEach(function(e){
    tmp2 = e.split('=');
        jsArry[tmp2[0]] = tmp2[1];
    });
    return jsArry;
}

and the fiddle demo http://jsfiddle.net/oswspkL0/ 和小提琴演示http://jsfiddle.net/oswspkL0/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM