Mina
Mina

Reputation: 23

Convertion from array to json array isnt made properly using vb.net?

I am trying to convert an array into a json array. What I have is:

I have created a class where I declare the fields that I am going to use.

Public Class Response
    Public container As Array
    Public signature As Tuple(Of Object, String, Integer, String, Object, String)

    Sub New()
        Me.container = Nothing
        Me.signature = Nothing
    End Sub

    Sub New(ByVal container As Array,
            ByVal signature As Tuple(Of Object, String, Integer, String, Object, String))
        Me.container = container
        Me.signature = signature
    End Sub
End Class

And the function where I want to convert them in JSON in order to use:

Public Function GetResponse()
    Dim response As New Response

    response.container = {"none", False}
    response.signature = New Tuple(Of Object, String, Integer, String, Object, String)({10, 10}, 
        "IT", 1, "Testing", {100, 100}, "Test Signature")
    Dim JSONString As String = JsonConvert.SerializeObject(response)

    Return JSONString
End Function

What I want it to look like is:

        { "container": {
            "type": "none",
            "single": false
        },
      "signature": {
            "coordinates": {
                "x": 10,
                "y": 10
            },
            "location": "IT",
            "page": 1,
            "reason": "Testing",
            "size": {
                "height": 100,
                "width": 100
            },
            "value": "Test Signature"
            }
    }

But what it looks like is:

{
  "container": [
    "none", false
  ],
  "signature": {
    "Item1": [10, 10],
    "Item2": "IT",
    "Item3": 1,
    "Item4": "Testing",
    "Item5": [100, 100],
    "Item6": "Test Signature"
  }
}

I am new to this, I would appriciate any help :) Thanks in advance!

Upvotes: 0

Views: 195

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

That's the problem with using a Tuple; the properties don't have names you can choose. If you don't want to create a full on class for this data (and take a google for "Paste JSON as Classes"; there isn't much excuse for not having them), use an anonymous type:

Dim response = New With { _
  .container = New With { .type = "none", .single = false }, _
  .signature = New With { _
    .coordinates = New With { .x = 10, .y = 10 }, _
    .location = "IT", _

    .someOtherName = someOtherValue, _

    ... etc ...

}

Upvotes: 2

Related Questions