将json数组字符串转换为json数组对象(值是json对象的数组)

var str1 ='[{"name":"kevin","age":18},{"name":"rose","age":16}]';

String.prototype.transArray=function(){
     var str =this.replace(/\[/,'');
    var newstr = str.replace(/\]/,'');
    var arr1 = newstr.split(',');
   var jsonstr,arr2=[];
     for(var i=0;i<arr1.length;i++){
    jsonstr = arr1[i]+','+arr1[i+1];
   arr2.push(JSON.parse(jsonstr));
    i++;
}
return arr2;
}
var k = str1.transArray();
console.log(k);

posted @ 2015-03-17 03:17  向着太阳生  阅读(1510)  评论(0编辑  收藏  举报