作为一名新手程序员,我正在使用VB.Net代码构建一个aspx页面,并尝试使用来自我们数据库的数据创建D3图表,但是我无法将返回值转换成正确的格式。
在我的VB代码中,我可以将字符串转换为我想要的确切格式:
[{ name: "PROAIR", value: 7}],{ name: "NASONEX", value: 4}]
但是当我传回它时,最终结果变成了:
{"d":"[{name: \"PROAIR\", value: 7},{name: \"NASONEX\", value: 4}]"}
我现在知道它尝试对返回值进行序列化,所以我尝试使用datatable并对其进行序列化,然后尝试使用List (Of Object)进行序列化,但每次都会得到不同的结果,并且返回值中有更多的引号和反斜杠。 我查看了Microsoft Person示例和一些Stack Contacts示例,但我真的需要构建一个全新的类并向其中添加新项吗?
我的字符串生成器代码如下:Dim sData As String
Dim sqlCmd As New SqlCommand(strSql, cn)
Using sdr As SqlDataReader = sqlCmd.ExecuteReader()
Dim sb As New StringBuilder()
sb.Append("[")
While sdr.Read()
sb.Append("{")
System.Threading.Thread.Sleep(50)
sb.Append(String.Format("name: '{0}', value: {1}", sdr(0), sdr(1)))
sb.Append("},")
End While
sb = sb.Remove(sb.Length - 1, 1)
sb.Append("]")
sData = sb.ToString
sData = sData.Replace("'", ControlChars.Quote)
cn.Close()
Return sData
End Using
如果我尝试对该字符串进行序列化:
Dim serializer As New JavaScriptSerializer()
Dim SerializedResult = serializer.Serialize(sData)
Return SerializedResult
我得到的结果是:
{"d":"\"[{name: \\\"PROAIR\\\", value: 7},{name: \\\"NASONEX\\\", value: 4}]\""}
我尝试使用以下代码进行反序列化:
Dim deserializedResult = serializer.Deserialize(Of String)(sData)
但是它报错了,显示数组类型失败,所以显然你不能这样做 :)在aspx页面中,我已经硬编码了:
success: function (r) {
var data = [{ name: "PROAIR", value: 7}],{ name: "NASONEX", value: 4}]
我的图表构建得非常完美,所以只需要以正确的格式返回SQL数据并更改脚本代码为var data = r;
编辑 下面是可工作的代码:
aspx页面:
$(function () {
GetData();
});
function GetData() {
$.ajax({
type: "post",
url: "D3PieChart.aspx/GetData",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
data = r.d;
data = jQuery.parseJSON(data)
-- Build D3 chart commands in this section --
},
error: function (xhr, status, error) {
OnFailure(error);
alert('Error');
}
});
}
</script>
VB页面:
<WebMethod()> _
Public Shared Function GetData() As String
Dim sConnect As String = ConfigurationManager.ConnectionStrings("SQLConnectionString").ConnectionString
Dim cn As New SqlConnection(sConnect)
Dim strSql As String = "SELECT name and a count of stuff "
strSql += "FROM a bunch of tables WHERE stuff = stuff "
Dim dtb As New DataTable
cn.Open()
If (cn.State And ConnectionState.Open) <> 0 Then
Dim sqlCmd As New SqlCommand(strSql, cn)
Dim sqlDad As New SqlDataAdapter(sqlCmd)
sqlDad.Fill(dtb)
dtb.Columns(0).ColumnName = "Name"
dtb.Columns(1).ColumnName = "value"
Dim sjData As String = GetJson(dtb)
Return sjData
End If
Return -1
End Function
Public Shared Function GetJson(ByVal dt As DataTable) As String
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
serializer.MaxJsonLength = Integer.MaxValue
Dim rows As New List(Of Dictionary(Of String, Object))()
Dim row As Dictionary(Of String, Object) = Nothing
Dim row2 As Dictionary(Of Integer, Object) = Nothing
For Each dr As DataRow In dt.Rows
row = New Dictionary(Of String, Object)()
row2 = New Dictionary(Of Integer, Object)()
For Each dc As DataColumn In dt.Columns
row.Add(dc.ColumnName.Trim(), dr(dc))
Next
rows.Add(row)
Next
Return serializer.Serialize(rows)
End Function
End Class
这是一个简单的图表,没有任何参数,所有内容都是硬编码的,但它从SQL Server数据库中获取图表数据。