Results 1 to 4 of 4

Thread: reproduce a particular JSON format

  1. #1
    moraleslos is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2013
    Posts
    2
    Reputation
    0

    reproduce a particular JSON format

    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?

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,080
    Reputation
    2802
    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.

  3. #3
    moraleslos is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2013
    Posts
    2
    Reputation
    0
    I need to return an instance of a class-- let's call this Clazz. Clazz looks like this:

    Code:
    <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:

    Code:
        <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):

    Code:
    >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:

    Code:
    GetClazzResult : {
              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?

  4. #4
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.5
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    15,080
    Reputation
    2802
    Quote Originally Posted by moraleslos
    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:
    Quote Originally Posted by Firefox browser window
    {"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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •