首页 > Web开发 > 详细

MapStr或者ListMapStr为参数,输出jsonObj或者Arr

时间:2018-03-14 12:37:35      阅读:69      评论:0      收藏:0      [点我收藏+]

标签:MapStr;ListMapStr为

//邓聪
//MapStr或者ListMapStr为参数,输出jsonObj或者Arr
co.mapStrOrListToObjOrArr = function (mapStrOrListMapStr) {
var strings = mapStrOrListMapStr.split("{");
if(strings.length===2){
var obj={}
var split = strings[1].split("=");
var string = "{"
for(var j = 0;j<split.length; j++){
if(j===0){
string +="\""+split[j]+"\""+":"
}
if (j===split.length-1){
string += "\"" + split[j].substring(0,split[j].length-1)+ "\""
}
if (j!=0 && j!=split.length-1){
var number = split[j].lastIndexOf(",");
var value = split[j].substring(0,number).trim()
string += "\"" + value + "\"" + ","
var key = split[j].substring(number+1,split[j].length).trim()
string += "\"" + key + "\"" + ":"
}
}
string+="}"
obj = JSON.parse(string)
return obj;

}else if(strings.length>2){
    var objArrStr = []
    for(var i =0 ;i<strings.length;i++){
        if(i>0 && i!= strings.length-1){
            objArrStr.push(strings[i].substr(0,strings[i].length-3))
        }else if(i>0){
            objArrStr.push(strings[i].substr(0,strings[i].length-2))
        }
    }
    var objArr = []
    for(var i=0;i<objArrStr.length;i++){
        var string = "{";
        var split = objArrStr[i].split("=");
        for(var j = 0;j<split.length; j++){
            if(j==0){
                string +="\""+split[j]+"\""+":"
            }
            if (j==split.length-1){
                string += "\"" + split[j] +  "\""
            }
            if (j>0 && j!=split.length-1){
                var number = split[j].lastIndexOf(",");
                var value = split[j].substring(0,number).trim()
                string += "\"" + value +  "\"" + ","
                var key = split[j].substring(number+1,split[j].length).trim()
                string += "\"" + key +  "\"" + ":"
            }
        }
        string+="}"
        objArr.push(JSON.parse(string))
    }
    return objArr;
}
return null;

}

MapStr或者ListMapStr为参数,输出jsonObj或者Arr

标签:MapStr;ListMapStr为

原文:http://blog.51cto.com/12165865/2086381

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号