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

时间:2014-11-09 23:43:44

标签: javascript arrays json string

如何将看起来像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

我会用& nfl_s_left2 =拆分拆分,但参数每次都会增加1。因此,我可以通过& nfl_s_left2 =进行拆分,但这只会拆分一次,因为下一个类似的是& nfl_s_left3 =。所以我想一个更具体的问题是我可以用变量字符串拆分吗? 我可以通过& nfl_s_left进行拆分但是我得到了数组值中的数字=。

清理这些数据的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

这个怎么样

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

和小提琴演示http://jsfiddle.net/oswspkL0/