0

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!

3
  • Please do not tag spam, it could get undesired atention Commented Oct 28, 2020 at 14:36
  • 2
    Where would the serializer get property names like "coordinates" from a Tuple? Also, it looks like you want container to be an object, not an array. Define objects with the shape you want your JSON to have, then fill in the data, and finally serialize. Commented Oct 28, 2020 at 14:36
  • 2
    Sigh... I give the same answer like in your previous question. Get rid of that tuple and create proper clases and your JSON problem will disappear too. Commented Oct 28, 2020 at 15:40

1 Answer 1

2

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 ...

}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.