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

Posted

技术标签:

【中文标题】从 SQL Datareader JavaScript 解析和格式化 JSON 数组【英文标题】:Parsing and formatting JSON array from SQL Datareader JavaScript 【发布时间】:2018-05-22 22:41:30 【问题描述】:

我正在从 datareaderin c# 填充 javascriptarray。 c# 代码在这里:

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';
'

这会产生我需要的谷歌地图输出

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

但是当我尝试对数据进行循环时,我正在努力使其工作。我的尝试之一是这样的:

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;

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

这是一个基于请求的堆栈 sn-p,我以前没有使用过,但我确实看到我的代码正在生成错误。那应该会有所帮助。我也会继续关注这一点。

<!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>

【问题讨论】:

C# 代码在哪里停止,而 javascript 代码从哪里开始?你能把(客户端,我假设)javascript作为StackSnippet吗? 我在原始帖子中添加了 stackSnippet。它产生了一个有用的错误。我会审查的。 如果您将 javascript 分离到 sn-p 的 Javascript 框中,然后单击“整理”按钮,则有一两个语法错误会立即显现出来。另外,here's more on JS debugging. 我稍微清理了代码 sn-p 并没有收到错误(我在副本/过去遗漏了一些内容),但仍然没有输出。 如果您在客户端难以将数据转换为正确的格式,为什么不从 c# 以正确的格式返回呢? 【参考方案1】:

你可以使用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;

【讨论】:

太棒了,谢谢。它工作得很好。在看到这个之前,我还能够按照 dbc 的建议通过在 C# 中创建正确的字符串来获得一个可行的解决方案,但这有助于我更好地理解 javascript 方面。

以上是关于从 SQL Datareader JavaScript 解析和格式化 JSON 数组的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server锁定的DataReader行为

使用 SQL Server 锁定的 DataReader 行为

使用 SQL DataReader 中的变量 [重复]

Sql Datareader 空值

无法从 .NET OleDB.DataReader 检索行

SQL Server DataReader.GetField 返回 null 而不是地理数据