CavFife
CavFife

Reputation: 1

Created nested json object in vb.net

I am unable to create a json object with nested objects. See the code and the current output versus the desired output.

Goal Output:

{
 "carlist" : [
 {
  "attributes": [
     {
      "key":"MAKE",
      "value":"Honda"
     },
     {
      "key":"MODEL",
      "value":"S2000"
     }
   ]
  }
 ],
"driver": "test",
"year":2001,
"color":"test"
}

What I have so far...

Public Class attributes
   Public Property key as string
   Public Property value as string
End Class

Public Class GetCars
   Public Property Carlist As List(Of attributes)
   Public Property driver as String
   Public Property year as Integer
   Public Property color as String

Dim cars as New List(Of attributes) From {
  New attribute() With {.key = "MAKE", .value = "Honda"},
  New attribute() With {.key = "MODEL", .value = "S2000"})

Dim car output as New GetCars() With {
 .Carlist = cars,
 .driver = "test",
 .year = 2001,
 .color = "test"}

Dim options as New JsonSerializerOptions() With {.IncludeFields = True}
Dim JSONString = System.Text.Json.JsonSerializer.Serialize(car, options)

I am unable to figure out how to have attributes object with values nest within the Carlist object so that it appears at the goal.

"Carlist" : [
 {
   "attributes" : [
     {
      make and model here
     }
    ]
   }
  ]

I get:

{
  "carlist" : [
   {
    "key":"MAKE",
    "value":"Honda"
   },
   {
    "key":"MODEL",
    "value":"S2000"
   }
  ],
  "driver": "test",
  "year":2001,
  "color":"test"
}

How do I get the attributes within the Carlist level?

Upvotes: 0

Views: 47

Answers (0)

Related Questions