1. 程式人生 > >MapStr或者ListMapStr為參數,輸出jsonObj或者Arr

MapStr或者ListMapStr為參數,輸出jsonObj或者Arr

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