我正在使用以下代码段来处理JSON
请求。
<WebMethod(Description:="Gets the Categories")> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Function GetCategories() As String
Using ds As DataSet = db.ExecuteDataSet(CommandType.Text, "SELECT NodeID,NodeName FROM Nodes WHERE ParentID='1'")
' Create a multidimensional jagged array
Dim JaggedArray As String()() = New String(ds.Tables(0).Rows.Count - 1)() {}
Dim i As Integer = 0
For Each rs As DataRow In ds.Tables(0).Rows
JaggedArray(i) = New String() {rs("NodeName").ToString(), rs("NodeID").ToString()}
i = i + 1
Next
' Return JSON data
Dim js As New JavaScriptSerializer()
Dim strJSON As String = js.Serialize(JaggedArray)
Return strJSON
End Using
End Function
我得到的回复:
<string xmlns="http://localhost:54511/">
[["Books","BK01"],["Computers","CO01"],["Gaming","GA01"],["Mobile & Accessories","MO01"]]
</string>
为什么我得不到key
??
例如:
<string xmlns="http://localhost:54511/">
[{"nodename":"Books","nodeid":"BK01"},{"nodename":"Computers","nodeid":"CO01"},{"nodename":"Gaming","nodeid":"GA01"},{"nodename":"Mobile & Accessories","nodeid":"MO01"}]
</string>
答案 0 :(得分:1)
那是因为你正在序列化一个数组而不是一个对象,对vb.net不是很熟悉,但我相信你要找的是Dictionary( Of String, String)
并将值添加为dictionary.add('nodename', rs("NodeName").ToString()
......等等