我有一组 JSON 数据,可以在来自 Javascript 的 console.log 中看到,在控制台中看起来像这样:
0: {datum: "18:17:00 04.08.2020", winddir: "S", windspeed: "28.7", windgust: "27.8", temp: "17.2"}
1: {datum: "18:16:00 04.08.2020", winddir: "NW", windspeed: "51.5", windgust: "32.9", temp: "17.2"}
2: {datum: "18:15:00 04.08.2020", winddir: "N", windspeed: "52.5", windgust: "32.2", temp: "17.3"}
3: {datum: "18:14:00 04.08.2020", winddir: "W", windspeed: "38.8", windgust: "28.4", temp: "17.3"}
4: {datum: "18:13:00 04.08.2020", winddir: "NW", windspeed: "30.2", windgust: "36.3", temp: "17.2"}
5: {datum: "18:12:00 04.08.2020", winddir: "N", windspeed: "31.6", windgust: "29.5", temp: "17.3"}
这是生成 JSON 数据的 javascript:
<script type="text/javascript">
var table = document.getElementById("weather");
var tableArr = [];
for (var i = 1; i < 100; i++) {
tableArr.push({
datum: table.rows[i].cells[0].innerHTML,
winddir: table.rows[i].cells[1].innerHTML,
windspeed: table.rows[i].cells[2].innerHTML,
windgust: table.rows[i].cells[6].innerHTML,
temp: table.rows[i].cells[8].innerHTML,
tempchill: table.rows[i].cells[9].innerHTML
});
}
console.log(tableArr);
</script>
这一切都很好,但现在我想用下面的 vbscript asp 调用将这些插入到 MSSQL 数据库中:
Set ObjRS = Server.CreateObject("ADODB.Recordset")
SQL="DECLARE @json nvarchar(max) set @json ="& tableArr &" Select * FROM OPENJSON(@json) WITH (datum datetime, winddir nvarchar(50), windspeed decimal(10,2), windgust decimal(10,2), temp decimal(10,2), tempchill decimal (10,2))"
Response.Write(SQL)
ObjRS.Open SQL, ObjDb, adOpenStatic, adLockReadOnly
当我将 SQL 调用打印到 HTML 中时,此“& tableArr &”为空,因此 SQL 调用失败。这个 SQL 调用只是为了查看 HTML 中的数据,但如果可行,那么我将直接将其插入 DB。
感谢所有提示!马丁