日期:2014-05-17  浏览次数:20478 次

从数据库取得数据保存为字符串传到前台怎么拼接字符串?
我把数据库里的数据存为字符串形式传到前台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(){
   map.clearOverlays();       
   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('|');
   alert(time); 
   var count = pointX.length;
   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]});
    }
    alert(_json);
    return _json;
}



我还把title,content等单个测试了一遍可以拼接,但是组合在一起alert出来的就是没有值,是[object object][object object][object object]
是什么原因呢?有没有高手帮忙解决一下,谢谢

------解决方案--------------------
alert(escape(_json.key)) 试试这个看看

key指title,content,point 这些字段名

如果上面解决不了,把这里的双引号改为引号再试试 

'title':arr1[i],'content':arr2[i],'point':arr3[i]+'|'+arr4[i]
------解决方案--------------------
假设:
string json = "{"root":[{ "tm_id":"1","tm_name":"zc"},{ "tm_id":"2","tm_name":"ww"}]}";


var dataObj = eval("(" + json+ ")"); //转换为json对象

alert(dataObj.root[0].tm_id);
alert(dataObj.root[0].tm_name);

alert(dataObj.root[1].tm_id);
alert(dataObj.root[1].tm_name);

ok!
------解决方案--------------------
字符串拼接