var s = '{"身高":"175cm","性别":"男","兴趣":"唱歌","兴趣":"棒球","兴趣":"游泳"}'
var kv = {}, m, reg = /"[^"]+":"[^"]+"/gi;
var m = s.match(reg);
var o={};
for(var i=0;i<m.length;i++){
kv=m[i].match(/"[^"]+"/gi);
var _arr1=[];;
for(var j=0;j<kv.length; j++){
_arr1.push(kv[j].replace(/"/g,''));
}
if(_arr1[0] in o){
if(typeof(o[_arr1[0]])=='string')
{
o[_arr1[0]]=[o[_arr1[0]]]
};
o[_arr1[0]].push(_arr1[1])
}else{
o[_arr1[0]]=_arr1[1]
}
}
console.log(o)//'{"身高":"175cm","性别":"男","兴趣":["唱歌,"棒球","游泳"]}'
json对象相同key合并value值
最新推荐文章于 2022-08-24 17:57:51 发布