日期:2014-05-16  浏览次数:20364 次

字符串分割以后怎么组成目标数组呢?
我把数据库里的数据存为字符串形式传到前台js,分割后该怎么修改成下面这种数组呢
var temp = [
{title:"奥亚酒店",content:"北苑路169号",point:"116.422792|40.009471"}]
C# code

OracleDataReader odr = cmd.ExecuteReader();
            int count = 0;
            while (odr.Read())
            {
                Longitudes[count] = System.Double.Parse(odr["x"].ToString());
                longitude += Longitudes[count] + "|";//将读取的经纬度存储为字符串形式  
                Latitudes[count] = System.Double.Parse(odr["y"].ToString());
                latitude += Latitudes[count] + "|";
                Custids[count] =Convert.ToInt16(odr["custid"]);
                custid += Custids[count] + "|";
                username += odr["username"].ToString() + "|";
                address += odr["address"].ToString() + "|";
                time += odr["starttime"].ToString() + "|";
                count = count + 1;
            }


字符串分割
JScript code

function secRing(){
   var array1 = "<%=longitude%>"; 
   var array2 = "<%=latitude %>";
   var array3="<%=username%>";
   var array4="<%=address %>";
   var array5="<%=time %>";
   var pointX = array1.split('|');
   var pointY = array2.split('|');
   var title = array3.split('|');
   var content = array4.split('|');
   var time = array5.split('|'); 
}



------解决方案--------------------
1:你要么在服务器端就拼接好
2:js中拼接的话,因为title,content,pointX ,pointY 的数组长度都应该是等值的,则以任意一个的长度做一个遍历拼接

JScript code
   function secRing(){
   var array1 = "<%=longitude%>"; 
   var array2 = "<%=latitude %>";
   var array3="<%=username%>";
   var array4="<%=address %>";
   var array5="<%=time %>";
   var pointX = array1.split('|');
   var pointY = array2.split('|');
   var title = array3.split('|');
   var content = array4.split('|');
   var time = array5.split('|'); 
   var JsonType=getJson(title,content,pointX,pointY);
}

function getJson(arr1,arr2,arr3,arr4){
    var _json = new Array();
    for(i=0;i<arr1.length;i++)
    {
        _json.push({"title":arr1[i],"content":arr2[i],"point":arr3[i]+"|"+arr4[i]});
    }
    return _json;
}