0

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?

3
  • 1
    You're misinterpreting the Root object. The Root has a Collection of carlist objects, which, in turn, contains a Collection of attributes objects. So, you're missing the carlist Type -- Note that attributes is a Collection of Key/Values pairs, so the missing carlist Type can be just a List(Of Dictionary(Of string, string)) Commented Feb 27 at 14:38
  • Thank you. I was finally able to figure it out. I changed Carlist as a list of attributelist. Then attributelist has a property of attributes as List of CarAttributeList. I was then able to nest the attributes within carlist. Commented Feb 28 at 12:01
  • If you have found a solution that works for you, post it as an answer to your own question (explaining what the problem was and how the code presented in the answer solves the issue) Commented Feb 28 at 12:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.