views:

870

answers:

4

I'm trying to send a hashtable to my webservice using JSON. However, you can't send a dictionary-type object (for some reason).

A solution that apparently works is "List(Of KeyValuePair(Of String, String))", as output anyway:

<WebMethod()> _
Public Function test() As List(Of KeyValuePair(Of String, String))
    Dim ret As New List(Of KeyValuePair(Of String, String))
    ret.Add(New KeyValuePair(Of String, String)("foo", "5"))
    ret.Add(New KeyValuePair(Of String, String)("bar", "baz"))
    Return ret
End Function

Outputs (in json): [{"Key":"foo","Value":"5"},{"Key":"bar","Value":"baz"}]

When I want to resubmit this in javascript, I have to use this function:

<WebMethod()> _
Public Sub test2(ByVal obj As List(Of KeyValuePair(Of String, String)))
    Dim dummy As UInt16 = obj.Count
End Sub

I call it using this as JSON parameter string:

'{"obj": [{"Key":"foo","Value":"5"},{"Key":"bar","Value":"baz"}]}'

This returns in an error. Not an error per se, but the contents of 'obj' is a list that contains 2 items, but when I look at them in the debugger, both are Nothing (or null for you C# people).

How can I send paired data from javascript to my webservice?

A: 

try this...

'{"obj": {"foo":"5","bar":"baz"}}'
Tracker1
Doesn't work in combination with the List(of KeyValuePair()). Do I need another object type?
Tominator
IIRC Dictionary<string, string> matches up fairly well in MVC, not as familiar with the service call, or how things are passed in... does not quoting the string work? Sorry for being a bit vague here, just more familiar with JS than the communications piece, or what gets encoded how in the communications channel.
Tracker1
+1  A: 

If you change test2 to receive a List of Dictionary objects, I think you'll be good to go.

<WebMethod()> _
Public Sub test2(ByVal obj As List(Of Dictionary(Of String, String)))
    Dim dummy As UInt16 = obj.Count
End Sub
Rojo
A: 

[Partial Answer]: I had a similar problem, and ended up using this JSON library (http://www.json.org/js.html) and the JSON.stringify(yourJSONObj) to send an object back to my service.

My issue had to do with JSON not recognizing my object correctly, it was assuming some of my property names were JSON primitives. By using this call (JSON.stringify()) it fixed that issue.

You could do something like:

var data = JSON.stringify('{"obj": [{"Key":"foo","Value":"5"},{"Key":"bar","Value":"baz"}]}'
);

And then send the data object to your ASMX service.

Carl
A: 

If you're calling a webservice from .Net and need to pass it data in a JSON format, consider using JayRock (http://jayrock.berlios.de/) to convert things into JSON. Just reference the DLL, import the name space, create a JsonObject, fill it with stuff and export it to a string. Here is an example


Imports Jayrock.Json
Imports Jayrock.Json.Conversion

Public Function foo(hsh As HashTable) As String

  Dim jObj As New JsonObject()

  jObj.Put("_myHashTable", hsh)

  Return JsonConvert.ExportToString(jObj)

End Sub


If you need to bring data from the DOM to a webservice via AJAX, I'd use the Javascript library from json.org as mentioned above. You can easily turn a JSON string into a JsonObject with JayRock as well.

Justin