Forums ».NET » VB.NET » VB.NET How to parse json file and get values from it with different way. VB.NET How to parse json file and get values from it with different way. Rated by 0 users This post has 1 verified answer 2 Replies 1 Follower Suggest an Answer.
25 Oct 2017CPOL
Working with simple JSON objects & collections to Custom Converters and Transformations into .Net classes.
Introduction
Whilst JSON is a compact and easy to read cross-language storage and data exchange format, the flexibility that it offers sometimes requires some custom handling to parse the data.
If you are not familiar with JSON, then here is a definition from the official http://www.json.org:
JSON (JavaScript Object Notation) is a lightweight JSON field and method reference with conversion type.
The IntelliSense debug window for the File collection property correctly reflects the recursive deserialization of the different File types:
Recursive Deserialization
JSON object structures can be many node levels deep. Each node could have properties with their own custom JsonConverters. An example of this is the MovieDB above in the previous section Multi-Value Type Collections. Here is an extract for the
Person JSON node structure with the node collection known_for :The
JsonDataTypeConverter class above already supports recursive deserialization. Once the node object type is identified, we re-call the JsonHelper.ToClass<...>(...) method:And in the
Person class, we apply the JsonConverter attribute JsonDataTypeConverter:Here, we can see from the IntelliSense debug window for the
Person.KnownFor collection property and correctly reflects the recursive deserialization of the Movie class type:Data Transformation
Normally, to transform data, it is a two-step process. First, we convert the JSON data to .Net classes (34 classes in total in this case), then transform the data.
The last example that I have demonstrates how to transform the JSON data to a custom class collection in a single step. By doing this, we have reduced the number of classes required, in this case, from 34 to 4!
Here is what the end result will look like:
As there are too many data classes to post here, I will only discuss the JsonConverter used. You can view and run the sample project,
WpfApplicationRateLimitStatus which is included in the download. I have included both the standard and custom mapping classes.The
JsonApiRateLimitsConverter compresses the multiple classes into a simpler collection of data that is compatible with the application's requirements.And to use:
Summary
We have covered how to work with standard value types, custom value types, compress structures to types, and transform from the raw JSON data structure to class structures that support the needs of our applications. Newtonsoft's Json.NET, the Helper classes, and the custom JsonConverters makes working with JSON data clean and very easy.
Sample Applications
There are six (6) sample applications, both in C# and VB, included in the download:
- WinFormSimpleObject - Etsy Category
- WinForm, code-behind
- WinFormSimpleCollection - Etsy Categories
- WinForm, Datbinding, MVVM (simple)
- WpfPhoto - Flickr Photo Viewer
- WPF, MVVM
- JsonFlickrCollectionConverter, JsonFlickrContentConverter, JsonFlickrUnixDateContentConverter, JsonFlickrUriContentConverter, StringEnumConverter
- WpfMultiSearch - MovieDB MultiSearch result
- WPF, MVVM, Implicit Templates
- JsonDataTypeConverter, JsonPartialUrlConverter
- WpfApplicationRateLimitStatus - Twitter JSON data Transformation
- WPF, MVVM, DataGrid
- JsonUnixDateConverter, JsonApiRateLimitsConverter
- WpfFileExplorer - Google Drive File Explorer
- WPF, MVVM, TreeView with load-on-demand & custom template, Implicit Templates, Hierarchical DataBinding
- JsonDataTypeConverter, JsonGoogleUriContentConverter,
A download link for all the samples is provided above.
History
v1.0 - August 15, 2017 - Initial Release
v1.1 - August 16, 2017 - Added reference to Fiddler in the Viewers & Validators section

v1.2 - August 16, 2017 - Added new Recursive Deserialization section (thanks BillWoodruff for the suggestion); new download with minor update to C#/VB WpfMultiSearch sample
v1.3 - August 19, 2017 - Added new Google Drive File Explorer sample app to demonstrate another version of Multi-Value Type Collections with mutiple identifier keys for file type class support broken down in the Multi-Value Type Collections section
v1.4 - October 26, 2017 - Added table of contents for easy navigation; added code generator quicktype.io; added serialization library Utf8Json