I have a WCF service that receives a JSON payload.
For instance, a payload like this:
{ "a":"123", "b":"xyz" }
Works well with the following service method signature:
<OperationContract()>
Public Sub SomeMethod(ByVal a As Integer, ByVal b As String)
a
and b
are automatically casted to an Integer
and a String
respectively.
However, now I need to send a slightly more complex argument to the service method, one that isn't an Integer
or a String
:
{ "a":"123", "b":"xyz", "c":"[ { "key":"1", "val":"2" }, { "key":"2", "val":"3" } ]" }
Basically, c
is an array of objects containing key-value pairs (i.e. JSON objects). So, what type do I declare c
as in the service method signature? I'm not really familiar with VB, so I don't know what types are available. If possible, please also include an example of how to get the values out of whatever type you suggest.
I use System.Collections.ArrayList
for this and you can then iterate through it using a For Each
.
I don't know what you are using to deserialize your JSON but I use JavaScriptSerializer
, E.g...
Imports System.Collections
Imports System.Collections.Generic
Imports System.Web.Script.Serialization
and then...
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