MapStr或者ListMapStr为参数,输出jsonObj或者Arr
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MapStr或者ListMapStr为参数,输出jsonObj或者Arr相关的知识,希望对你有一定的参考价值。
//邓聪
//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的主要内容,如果未能解决你的问题,请参考以下文章
吴恩达深度学习:2.3梯度下降Gradient Descent