carls
4Penny.net
Points: 3300

11/2/2018 1:08:34 PM

I need to create a JSON string from my data.

* This article, and all our great .NET Development documentation, Is available on the .NET Development menu

I need to be able to return a JSON string containing the results of an SQL query.  I know there are 3rd party tools that can simplify this, but I wanted to not have to add more to my project.

 

I built a small WinForm project to determine how to convert text into a JSON-formatted string.  Even though my source data would be from an SQL results set, for my test I just generated some data.  You only have to change the Vendor.GetVendorInfo() to accommodate whatever means you use to supply your data.

It is important to note the decorations used in the Vendor and Vendors classes.  This is needed for the JSON serialization.

The Form1 class is just a WinForm with a Start and Exit button and a text box for displaying the resulting JSON string.  I first tried with a single Vendor instance, the v1 variable.  Once that was working, I moved on to an array of Vendor types (vendors).

The GetVendorInfo() just creates the data to be serialized.  The BuildJSON() method does the actual serialization.

NOTE: Make sure you add a reference to System.Runtime.Serialization to the project!

4Penny.net
Version: Unknown or N/A
Section: .NET Development
Table Definition Quick Links
All Tables
SOP Tables
RM Tables
GL Tables
POP Tables
HR Tables
PM Tables
UPR Tables
IV Tables
Olympic Tables
3