gpt4 book ai didi

javascript - 在 Javascript 中访问 JavaScriptSerializer

转载 作者:行者123 更新时间:2023-11-30 06:37:48 24 4
gpt4 key购买 nike

我有一个序列化的 json 数组,我如何在 javascript 中访问它。
我正在使用 javascript
amcharts 并想将 vb.net` 数组传递给 javascript 图表。代码如下:

VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DataSetToJSON()
End Sub
Public Function SelectChartData(ByVal Name As String) As DataSet
Dim sqlCmd As New SqlCommand
Dim myData As New DataSet

sqlCmd.CommandTimeout = 1200

sqlCmd.CommandType = CommandType.StoredProcedure
sqlCmd.CommandText = "SelectAmChartdata"

sqlCmd.Parameters.Add("@Name", SqlDbType.VarChar)
sqlCmd.Parameters("@Name").Value = Name

Dim da As New dsSV1TableAdapters.tblGeneralTableAdapter

da.Adapter.SelectCommand = sqlCmd

sqlCmd.Connection = da.Connection
sqlCmd.Connection.Open()
da.Adapter.Fill(myData)

sqlCmd.Dispose()
sqlCmd = Nothing

da.Connection.Close()
da.Connection.Dispose()
da.Connection = Nothing

Return myData

End Function

Public Function DataSetToJSON()
Dim dt As DataTable
ds = SelectChartData("Weather")


Dim dict As New Dictionary(Of String, Object)

For Each dt In ds.Tables
Dim arr(dt.Rows.Count) As Object

For i As Integer = 0 To dt.Rows.Count - 1
arr(i) = dt.Rows(i).ItemArray
Next

dict.Add(dt.TableName, arr)
Next

Dim json As New JavaScriptSerializer
Return json.Serialize(dict)

End Function

ASPX

<script type="text/javascript">

//var chartData = **Access Serverside Array**

window.onload = function () {
var chart = new AmCharts.AmSerialChart();
chart.dataProvider = chartData;
chart.categoryField = "country";
chart.marginTop = 15;
chart.marginLeft = 55;
chart.marginRight = 15;
chart.marginBottom = 80;
chart.angle = 30;
chart.depth3D = 15;

var catAxis = chart.categoryAxis;
catAxis.gridCount = chartData.length;
catAxis.labelRotation = 90;

var graph = new AmCharts.AmGraph();
graph.balloonText = "[[category]]: [[value]]";
graph.valueField = "visits"
graph.type = "column";
graph.lineAlpha = 0;
graph.fillAlphas = 0.8;
chart.addGraph(graph);

chart.write('chartContainer');
}
</script

最佳答案

您可以将序列化数据直接转储到 ASPX 页面中:

<script type="text/javascript">

var my_data = <%= DataSetToJSON() %>;

</script>

或者您可以在 ASPX 页面中使用:

<input type="hidden" id="my_data" clientidmode="static" runat="server" value="" />

在代码隐藏中:

my_data.Value = DataSetToJSON();

然后您可以使用 document.getElementById('my_id').value 提取数据,使用 JSON.parse 之类的方法对其进行解码,然后将其传递给您的图表。

关于javascript - 在 Javascript 中访问 JavaScriptSerializer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13451027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com