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

前端之家收集整理的这篇文章主要介绍了MapStr或者ListMapStr为参数,输出jsonObj或者Arr前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

//邓聪
//MapStr或者ListMapStr为参数,输出jsonObj或者Arr
co.mapStrOrListToObjOrArr = function (mapStrOrListMapStr) {
var search = mapStrOrListMapStr.indexOf("[");
var strings = mapStrOrListMapStr.split("{");
if(search === -1){
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(search != -1){
    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;
}

}

原文链接:https://www.f2er.com/json/288601.html

猜你在找的Json相关文章