【问题标题】:No key value in JSONJSON 中没有键值
【发布时间】:2012-09-17 10:14:58
【问题描述】:

我正在使用以下代码 sn-p 处理 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>

【问题讨论】:

    标签: asp.net vb.net json web-services


    【解决方案1】:

    那是因为您正在序列化一个数组而不是一个对象,对 vb.net 不是很熟悉,但我相信您正在寻找的是 Dictionary( Of String, String) 并将值添加到它作为 dictionary.add('nodename', rs("NodeName").ToString() ... 和以此类推

    【讨论】:

      猜你喜欢
      • 2019-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      相关资源
      最近更新 更多