js中将字符串[user[name=jjdf,age=1],user[name=ggdf,age=5]]转为json串对象

js中将字符串转为json串对象
var str = "[user[name=jjdf,age=1],user[name=ggdf,age=5]]";
string2json(str);
function string2json(str){
   str = str.substring(1,str.length-1);
   str = str.replaceAll(/\user/g,'')
   str = str.replaceAll("=","\":\"")
   str = str.replaceAll(/\[/g,'{"')
   str = str.replaceAll(/\]/g,'"}')
   str = str.replaceAll(/\,/g,'","')
   str = str.replaceAll(/\}"/g,'}')
   str = str.replaceAll(/\"{/g,'{')
   str = "["+str+"]"
   console.log(str)
   console.log(JSON.parse(str))
//    $.parseJSON( jsonstr );
//    eval('(' + jsonstr + ')');
}

你可能感兴趣的:(javascript,前端,开发语言)