reproduce a particular JSON format

moraleslos

New member
Joined
Apr 30, 2013
Messages
2
Programming Experience
3-5
I'm using WCF on my VB services to return JSON and am running into issues where I can not reproduce a particular format. The JSON I would like returned looks something like:

{
tuples: [​
[[north,1.8,2.5],[south,1.3,-1.4],[east,1.1,0]],
[[north,1.9,2.4],[west,10.34,4.76]]​
]​
}

If I used a class that contains a List(Of List(Of String)) or a multi-String array, to be returned by WCF the output always looks like:

{
tuples: [​
[0: north,1.8,2.5, 1: south,1.3,-1.4, 2: east,1.1,0],
[0: north,1.9,2.4, 1: west,10.34,4.76]​
]​
}

The JSON translation adds the step of the list/array into the JSON string.

Any ideas on how to produce the appropriate JSON output with WCF and VB?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
What you have there is a jagged array of object arrays, or a list(of object) nested three times. I have no problem producing that output and I don't see the indexes you mentioned using any kind of list or array.
Here's a repro, start with this basic Wcf Json service: Mikes Knowledge Base
Add to contract:
    <OperationContract()>
    <WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.Wrapped, UriTemplate:="getValues")>
    Function GetValues() As <MessageParameter(Name:="tuples")> Object()()

Add implementation:
    Public Function GetValues() As Object()() Implements IService1.GetValues
        Dim ar(1)() As Object
        ar(0) = {New Object() {0, "a1", False}, New Object() {0, "a2", False}}
        ar(1) = {New Object() {0, "b1", False}, New Object() {0, "b2", False}}
        Return ar
    End Function

Sample output:
{"tuples":[
[[0,"a1",false],[0,"a2",false]],
[[0,"b1",false],[0,"b2",false]]
]}
By the way, I do suspect it is just your test client that is outputting the item indexes, as well as the "pretty" formatting, of the Json data.
 

moraleslos

New member
Joined
Apr 30, 2013
Messages
2
Programming Experience
3-5
I need to return an instance of a class-- let's call this Clazz. Clazz looks like this:

VB.NET:
<DataContract()> _
Public Class Clazz
 Private _tuples As Object()()
 Private _id As ClazzId


 <DataMember()> _
    Public Property tuples() As Object()()
        Get
            Return _tuples
        End Get
        Set(ByVal value As Object()())
            _tuples = value
        End Set
    End Property


    <DataMember()> _
    Public Property id() As ClazzId
        Get
            Return _id
        End Get
        Set(ByVal value As ClazzId)
            _id= value
        End Set
    End Property


Public Class ClazzId
        Private _realId As String

End Class

End Class


Now my WCF looks like this:

VB.NET:
    <OperationContract()> _
    <WebGet(ResponseFormat:=WebMessageFormat.Json, _
            BodyStyle:=WebMessageBodyStyle.Wrapped, _
            UriTemplate:="/GetClazz/{name}")> _
    <JSONPBehavior(callback:="method")> _
    Function GetClazz(ByVal name As String) As Clazz

When I execute this is what it returns (using Firefox debugger on the response invoked via JS):

VB.NET:
>response [object Object]
  > GetClazzResult [object Object]
    > tuples: [object Array]
       > 0: [object Array]
          0:  "a, b, c"
          1:  "d, e, f"
          2:  "g, h, i"
          ...
          ...
       > 1: null
...
...

Doing a write to console (console.log) of the above response gives me this:

VB.NET:
[FONT=Verdana]GetClazzResult [/FONT]: {
          tuples: [
               0 : [
                    0 : a, b, c,
                    1 : d, e, f,
                               ....
                            ],
                        1 : NULL
                ],
                 id : {
                     realId : 31
                }
}


So it is indeed adding the steps in the arrays in the Json output. Any other ideas?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,604
Location
Norway
Programming Experience
10+
moraleslos said:
using Firefox debugger on the response invoked via JS
What?

Entering the service url in a Firefox browser window returns the plain json data, and it looks nothing like that. As I said in last line of previous post, you're using some kind of client that adds lots of stuff (and removes some) to make it easier to read/debug for you, the actual returned json would look like this:
Firefox browser window said:
{"GetClazzResult":{"id":{"realId":"31"},"tuples":[["a, b, c","d, e, f","g, h, i"],null]}}
If you use Fiddler debugger you can select different views, the json view for example presents the data as a tree where you can expand/collapse the nodes. You can see the plain json in text/raw view, just like when using a browser.
 
Top Bottom