JSON is really easy once you get the hang of it, and incredibly powerful.
I started using JSON a while back when making my site’s Trakt API, but only recently started using it with a VB .Net program I was writing. It was so incredibly easy to use, and somewhat fun, that I wanted to talk a little about how to use JSON data when working with VB.Net.
What makes JSON so easy to use is that you’d give it a string like below:
In the example above, the array contains two JSON objects. Each object has two properties, a name and a birthdate. The name property has two sub-properties, a first name and a last name.
This type of functionality is very useful if you are able to capture data stored as text strings. Sometimes, you may require this functionality for VB.Net. Unfortunately, a JSON parser for VB.Net is not built in (as far as I’m aware), and so, you will need to use external libraries to get the functionality you need. So, I used Newtonsoft’s JSON .Net, which is available for download for free over here. What I’d like to do is cover how to use these libraries in your own VB.Net projects. Particularly, I’d like to cover how to handle strings that are being imported from other sources as a JSON string. That is, you don’t have an object ready to cast the string into. If you did have an object created in your code, the process becomes a lot easier, but I won’t be
Once you’ve downloaded the library, you’ll need to first import the correct files into your project before you can use them. To do this, navigate to the “Bin” subfolder in the archive and extract all the files to your project’s working directory. Inside Visual Studio, right click your project and then click “”Add Reference”. Navigate and select the DLL file you just extracted. If done correctly, you’ll be able to call functions from within this DLL.
There are lots of different things you can do with JSON data, too many to list out. But here are a few of the more common tasks you may be required to carry out using data. I’ll use the JSON data I put above for consistency.
Start by importing the respective namespace for ease:
Converting a JSON object back to a string:
Check if a property exists:
Change the value of a property:
Looping Through a JSON object:
Looping Through a JSON object (works exactly like an array):
Removing an item from a JSON object:
Removing property from an item:
Adding a JSON item:
Adding a property to an item:
Adding a property with children to an item:
I hope this post serves as a little help for people that want to get into using the Newtonsoft JSON library from the .Net framework, particularly for Visual Studio. A lot of the guides and forum posts online target C#, which has a slightly different syntax when dealing with objects, which may make it a little difficult to follow, especially for an amateur programmer. I had a problem learning this myself as well.
If there are any inaccuracies, mistakes, or ways I could better achieve what I was doing in the code above, please do let me know. I’m not an expert programmer, by any means, so what seems correct to me may not be the best or correct way of doing something.