繁体   English   中英

从SQL Datareader JavaScript解析和格式化JSON数组

[英]Parsing and formatting JSON array from SQL Datareader JavaScript

我正在使用填充数组。 代码在这里:

public string ConvertDataTabletoString() {
  DataTable dt = new DataTable();
  //
  SqlConnection conn = new SqlConnection();
  SqlCommand cmd = new SqlCommand();
  string connStr = ConfigurationManager.ConnectionStrings["FurnitureDB"].ConnectionString;
  conn.ConnectionString = connStr;
  conn.Open();
  cmd.CommandType = System.Data.CommandType.StoredProcedure;
  cmd.Connection = conn;
  cmd.CommandText = "GetLatLongPins_active";
  SqlDataReader datareader = cmd.ExecuteReader();
  //var dt = new DataTable();
  dt.Load(datareader);

  //con.Open();
  System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(cmd);
  da.Fill(dt);
  System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
  List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
  Dictionary<string, object> row;

  foreach(DataRow dr in dt.Rows) {
    row = new Dictionary<string, object>();
    foreach(DataColumn col in dt.Columns) {
      row.Add(col.ColumnName, dr[col]);
    }
    rows.Add(row);
  }

  return serializer.Serialize(rows);
}

返回的数组如下所示:

var markers = JSON.parse('[{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":40.554306,"Longitude":-81.921422,"Active":true},{"Latitude":40.906651,"Longitude":-97.093629,"Active":true},{"Latitude":29.431261,"Longitude":-97.171709,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":40.554306,"Longitude":-81.921422,"Active":true},{"Latitude":40.906651,"Longitude":-97.093629,"Active":true},{"Latitude":29.431261,"Longitude":-97.171709,"Active":true}]');

我需要解析它,以便Google Maps API看起来像这样:

var waypts = [{
  location: '34.257179, -119.234190',
  stopover: true
}, {
  location: '34.257179, -119.234190',
  stopover: true
}, {
  location: '40.554306, -81.921422',
  stopover: true
}, {
  location: '40.906651, -97.093629',
  stopover: true
}, {
  location: '29.431261, -97.171709',
  stopover: true
}, ]

我尝试了多种变体,如果将其切成仅一块,就可以创建所需的结果,例如:

var obj = JSON.parse('{"Latitude":34.257179,"Longitude":-119.234190,"Active":true}');
document.getElementById("demo").innerHTML = 'Location:' + obj.Latitude + ',' + obj.Longitude + ',' + 'stopover:true';
'

这将产生我需要Google Maps的输出

位置:34.257179,-119.23419,中途停留:true

但是,当我尝试遍历数据时,我正在努力使其发挥作用。 我的尝试之一如下所示:

var obj = JSON.parse('{"Latitude":34.257179,"Longitude":-119.234190,"Active":true}', {
  "Latitude": 29.431261,
  "Longitude": -97.171709,
  "Active": true
}]
');


for (i = 0; i < obj.length; i++) {
  x += obj.Latitude[i] + "<br>";
}

document.getElementById("demo").innerHTML = x;

我正在通过将输出发送到输出窗口进行测试。 当我运行上面的代码时,我什么也没得到,没有输出。 我试图将我的示例与许多可用示例相关联,但没有成功。 感谢您的任何建议。

这是一个基于请求的堆栈片段,我以前没有使用过,但是我确实看到我的代码正在生成错误。 那应该有帮助。 我也将继续研究。

 <!DOCTYPE html> <html> <body> <h2>Create Object from JSON String</h2> <p id="demo"></p> <script> var x = []; var obj = JSON.parse('[{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":40.554306,"Longitude":-81.921422,"Active":true},{"Latitude":40.906651,"Longitude":-97.093629,"Active":true},{"Latitude":29.431261,"Longitude":-97.171709,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":40.554306,"Longitude":-81.921422,"Active":true},{"Latitude":40.906651,"Longitude":-97.093629,"Active":true},{"Latitude":29.431261,"Longitude":-97.171709,"Active":true}]'); for (i = 0; i < obj.length; i++) { x += obj.Latitude[i] + "<br>"; } document.getElementById("demo").innerHTML = x; </script> </body> </html> 

您可以使用Array#map

然后,如果您输入的是:

var obj = JSON.parse('[{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":40.554306,"Longitude":-81.921422,"Active":true},{"Latitude":40.906651,"Longitude":-97.093629,"Active":true},{"Latitude":29.431261,"Longitude":-97.171709,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":40.554306,"Longitude":-81.921422,"Active":true},{"Latitude":40.906651,"Longitude":-97.093629,"Active":true},{"Latitude":29.431261,"Longitude":-97.171709,"Active":true}]');

通过使用:

var waypts = obj.map(function(x) {
  return {
    location: x.Latitude + ", " + x.Longitude,
    stopover: true
  };
});

结果将是:

[
  {
    "location": "34.257179, -119.23419",
    "stopover": true
  },
  {
    "location": "34.257179, -119.23419",
    "stopover": true
  },
  {
    "location": "40.554306, -81.921422",
    "stopover": true
  },
  {
    "location": "40.906651, -97.093629",
    "stopover": true
  },
  {
    "location": "29.431261, -97.171709",
    "stopover": true
  },
  {
    "location": "34.257179, -119.23419",
    "stopover": true
  },
  {
    "location": "34.257179, -119.23419",
    "stopover": true
  },
  {
    "location": "40.554306, -81.921422",
    "stopover": true
  },
  {
    "location": "40.906651, -97.093629",
    "stopover": true
  },
  {
    "location": "29.431261, -97.171709",
    "stopover": true
  }
]

像这样:

 (function() { var obj = JSON.parse('[{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":40.554306,"Longitude":-81.921422,"Active":true},{"Latitude":40.906651,"Longitude":-97.093629,"Active":true},{"Latitude":29.431261,"Longitude":-97.171709,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":34.257179,"Longitude":-119.234190,"Active":true},{"Latitude":40.554306,"Longitude":-81.921422,"Active":true},{"Latitude":40.906651,"Longitude":-97.093629,"Active":true},{"Latitude":29.431261,"Longitude":-97.171709,"Active":true}]'); var waypts = obj.map(function(x) { return { location: x.Latitude + ", " + x.Longitude, stopover: true }; }); console.log(waypts); })(); 
 .as-console-wrapper { position: absolute; top: 0; } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM