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

js如何读取json数据
读取数据的时候报错“编译错误: 缺少 ']'”,难道是格式不对?
C# code

 public string LoadData()
  {
  string sql = "select [Lat],[Lng],[strong] from [dbo].[Test_LatLng]";
  DataTable dt = database.Query(sql);
  if (dt != null && dt.Rows.Count > 0)
  {
  string str = "[";
  int i = 0;
  for (i = 0; i < dt.Rows.Count; i++)
  {
  str = str + "{Lat:" + dt.Rows[i]["Lat"].ToString()+",";
  str = str + "Lng:" + dt.Rows[i]["Lng"].ToString()+",";
  str = str + "strong:" + dt.Rows[i]["strong"].ToString() + "}";
  }  
  str = str + "]";
  return str;

  }

  else
  {
  return "";
  }  


读取
C# code
  function addmarkers() {
  var vb = Map.maptest.LoadData().value;
  var jsonData = eval("(" + vb + ")");
}


------解决方案--------------------
jsonobjectcollection arr=..
arr.add(new jsonstringvalue("a",a));
jsonutility.generateindentedjsontext=true;

.aspx
json=eval("");
json.a
------解决方案--------------------
var jsonData= eval("(" + data + ")");
------解决方案--------------------
探讨
for (i = 0; i < dt.Rows.Count; i++)
{
str = str + "{Lat:" + dt.Rows[i]["Lat"].ToString()+",";
str = str + "Lng:" + dt.Rows[i]["Lng"].ToString()+",";
str = str + "strong:" + dt.Rows[i]["strong"].ToString() + "}";
}

------解决方案--------------------
if (dt != null && dt.Rows.Count > 0)
{
string str = "[";
int i = 0;
for (i = 0; i < dt.Rows.Count; i++)
{
str = str + "{\"Lat\":\"" + dt.Rows[i]["Lat"].ToString()+"\",";
str = str + "\"Lng\":\"" + dt.Rows[i]["Lng"].ToString()+"\",";
str = str + "\"strong\":\"" + dt.Rows[i]["strong"].ToString() + "\"},";
}
str = str.TrimEnd(',')
str = str + "]";
return str;

}
内容若确定为数字,双引可不加