Generate a flexible JSON array in VB.NET

Looking to generate a flexible array that can be converted into a JSON array for visualization with Morris charts. The usual approach in VB.NET is as follows:

 Dim xArray(2)

 xArray(0) = New With {Key .TradingDay = "Day1", .Seller1 = 1500, .Seller2 = 1600}
 xArray(1) = New With {Key .TradingDay = "Day2", .Seller1 = 2300, .Seller2 = 1850}
 xArray(2) = New With {Key .TradingDay = "Day3", .Seller1 = 4970, .Seller2 = 3560}

However, the challenge here lies in the dynamic nature of the Sellers involved. Clients should have the choice to select sellers from a listbox. In such cases, the setup could look like this:

 Dim xArray(2)

 xArray(0) = New With {Key .TradingDay = "Day1", .Seller2 = 1600, .Seller3 = 2550, .Seller4 = 3600}
 xArray(1) = New With {Key .TradingDay = "Day2", .Seller2 = 1850, .Seller3 = 890, .Seller4 = 3456}
 xArray(2) = New With {Key .TradingDay = "Day3", .Seller2 = 3560, .Seller3 = 10890, .Seller4 = 2850}

Is there a method to construct a dynamic array and convert it into a JSON format successfully?

Answer №1

To serialize an array using Newtonsoft.Json, you need to include the Import Newtonsoft.Json statement in your code. The example provided below demonstrates how you can modify and adapt it to suit your specific requirements.

 Public Class Product
      Public Property TradingDay As String
      Public Property sellers As List(Of Seller)
      End Class

      Public Class Seller
      Public Property SellerName As String
      Public Property price As String
      </Class>

     Dim mainList as List(of Product)

     ' Your main for loop

        Dim product As New Product()
        product.TradingDay = "Day1"
        dim sellerList as List(of Seller)
            ' Loop through sellers
                dim seller as new Seller()
                seller.SellerName ="name should be here"
                seller.price="your price"
                sellerList.add(seller)
            next
        product.sellers = sellerList
        mainList.add(product)
      Next
      Dim output As String = JavaScriptConvert.SerializeObject(mainList)

Please note that there may be syntax errors in the provided code snippet since it has been a while since I worked with VB. However, this approach should guide you towards achieving your desired outcome.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to align two <select> elements side by side using Bootstrap

The issue I am encountering is that these two select elements within the same control-group are being displayed vertically when I want them to be displayed horizontally. I attempted using inline-block in CSS, but there are other <div> elements with ...

Modify object rotation animation direction using keyboard controls in Three.js

Adjusting the object rotation direction with key controls is within my capability by utilizing the following code: case 37: scene.rotation.x -= 0.01; break case 38: scene.rotation.z -= 0.01 break Nevertheless, the rotation remai ...

Ways to present a Nuxt page as a reply from an express route

How can I achieve a similar functionality to res.render on a Nuxt page? The project is using the nuxt-express template, which combines Nuxt and Expressjs. Although Nuxt provides nuxt.render(req, res) and nuxt.renderRoute, I am having trouble making it wo ...

What is the simplest method for converting a large JSON array of objects into an object containing the same data under the key "data" in TypeScript?

What is the most efficient method for converting a large JSON array of objects into an object with a key named "data" using TypeScript: Original Format: [ { "label":"testing", "id":1, "children":[ { "label":"Pream ...

Verify whether all the elements within a specific div are distinct by utilizing jQuery

I need to create a select box that, when an option is chosen, generates a certain number of textboxes within a div. Currently, there are three fields: display_name[], user_name[], and user_password[] The code for this functionality is as follows: <se ...

What is the best way to increase a numerical input?

While experimenting with HTML input elements, I decided to utilize the step attribute. This allowed me to increment the current value by 100 when clicking the up or down arrows in the input field. However, I discovered that the step attribute restricts th ...

Learning to transform EST time data into local time within JavaScript utilizing moment.js and Date objects

The recorded date and time is "03/19/2020 13:15:00" in Eastern Standard Time (EST). When attempting to convert it to the local timezone, I have tried various methods, such as using moment.js to change the format from 'MM/DD/YYYY HH:mm:ss' to UT ...

Encountering a bindings issue when trying to utilize libxml-xsd within an electron application

Seeking guidance on validating an XML file against its schema within an electron application. Prior to adding the 'libxml-xsd' require statement to my angular2 service, it functioned properly. However, upon inclusion of this statement: const xs ...

Having trouble maintaining the initial state of the first accordion in a foreach loop

I have a collapsible accordion here, which is functioning well with static data. Now, I have connected it to a database and am attempting to retrieve data from the database. Below is the code I have implemented so far in CodeIgniter, View: <div class= ...

Exporting JSON data to CSV or XLS does not result in a saved file when using Internet Explorer

Presented below is the service I offer: angular.module('LBTable').service('exportTable', function () { function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel, fileName) { //If JSONData isn't an object, parse the ...

Utilizing JSON Files with C++ Programming

Currently, I am attempting to parse a JSON file using the jsoncpp library. Nevertheless, I am finding it challenging to comprehend its documentation. Can someone simplify and explain its functionality in layman's terms for me? Let's consider a s ...

How can I incorporate percentage values into input text in Angular?

How can I include a percent sign in an input field using Angular, without relying on jQuery? I am looking for a solution that is identical to what I would achieve with jQuery. Here is the current status of my project: ...

Issue with populating labels in c3.js chart when loading dynamic JSON data

Received data from the database can vary in quantity, ranging from 3 to 5 items. Initially, a multi-dimensional array was used to load the data. However, when the number of items changes, such as dropping to 4, 3, 2, or even 1, the bars do not populate acc ...

The styling of MUI components adapts when the Navigate component in React Router is initialized

Incorporating React Router into my application has led to an unexpected side-effect while implementing the <Navigate to="/"> component (which goes to <AthleteHomepage />) upon state change. Currently, I haven't set up dynamic sta ...

Utilize Oracle 18 to efficiently extract data from JSON using the json_table function

Here is a sample request: with j (sJson) as ( select '{ "ID":"1444284517", .... )) jt In column, different fields may be present. How can all fields in columns be listed without specifying the type a ...

Using anchor tags to send HTML code through email

I currently have an anchor tag on my website that looks like this: <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e0d11131b11101b3e0d11131b09161b0c1b501d1113">[email protected]</a>?subject=Wel ...

The loop is not fully completing all actions during the first iteration

While working on a multiple upload feature using the Multiple_upload library in CodeIgniter, I encountered an issue where the loop that generates thumbnails only runs on the first iteration. Here is the relevant code snippet: function saveContentImages() ...

The values in my array are causing it to output NAN

I'm currently working on a program that aims to combine a variable number of one-row matrices with varying lengths. The goal is to add the elements of each matrix together, where element one of array one adds to element one of array two, and so forth. ...

Save user sessions in a database using node.js, express, and mongoose-auth

I have a question about authentication and sessions in node.js. So, I've set up authentication using express.js and mongoose-auth with mongodb: app.use(express.cookieParser()); app.use(express.session({ secret: 'esoognom'})); app.use(auth. ...

How to send parameters to the jQuery delete button click event handler

Here is the jQuery code I am working with: $('#btnDelete').click( function() {//Do the delete here via jquery post}); In my table, each row has a delete button like this: <a id="btnDelete">Delete</a> I need to pass parameters to t ...