0

我有一个接收 JSON 有效负载的 WCF 服务。

例如,像这样的有效负载:

{ "a":"123", "b":"xyz" }

适用于以下服务方法签名:

<OperationContract()>
Public Sub SomeMethod(ByVal a As Integer, ByVal b As String)

ab分别自动转换为 anInteger和 a String

但是,现在我需要向服务方法发送一个稍微复杂一点的参数,它不是 anInteger或 a String

{ "a":"123", "b":"xyz", "c":"[ { "key":"1", "val":"2" }, { "key":"2", "val":"3" } ]" }

基本上,c是一个包含键值对的对象数组(即 JSON 对象)。c那么,我在服务方法签名中声明什么类型?我对VB不是很熟悉,所以我不知道有哪些类型可用。如果可能,还请提供一个示例,说明如何从您建议的任何类型中获取值。

4

1 回答 1

1

我使用System.Collections.ArrayList它,然后您可以使用For Each.

我不知道你用什么来反序列化你的 JSON,但我使用JavaScriptSerializer,例如..

Imports System.Collections
Imports System.Collections.Generic
Imports System.Web.Script.Serialization

进而...

Dim sJSON as String = "{}" 'Contains the JSON in your question
Dim jsSerializer As JavaScriptSerializer = New JavaScriptSerializer()

Dim dictData As Dictionary(Of String, Object) = jsSerializer.Deserialize(Of Dictionary(Of String, Object))(sJSON)

If dictData.ContainsKey("c") Then
    If TypeOf dictData("c") Is ArrayList Then
        Dim arrData As ArrayList = DirectCast(dictData("c"), ArrayList)
        For Each arrDataRecord In arrData
        Next
    End If
End If
于 2013-01-29T16:06:10.760 回答