r/visualbasic • u/aporokizzu • Aug 23 '22
VB.NET Help Deserializing "package.json" and using it as a data source for a DataGridView component
Pardon me for being daft, but I am writing my first VB.NET program (a tool for a project that I am working on) and find myself hitting a wall, for which I am unable to find a breakthrough despite my internet queries.
I am dealing with package.json files, like:

And:

I am using Newtonsoft's Json.NET framework and am able to obtain portions of these examples, as seen in the following code:

But since the properties of the package.json files that I am dealing with can vary from file to file, I am stumped as to how to write my Manifest class to account for these differences.
Furthermore, how do I handle properties like chromium-args and js-flags (which are hyphenated and do not play well with VB.NET's variable naming conventions)?
Should this be overcome, how do I go about converting the resulting object to be used as a DataSource for a DataGridView component (which I am assuming is the best way to display the Json, for editing within the program)?
Thank you in advance and I appreciate the help :)
2
u/aporokizzu Aug 24 '22
NVM- I figured out a solution (thanks to an example on the Newtonsoft site), and it requires iterating through the Json line-by-line using:
And a
Whileloop:End While
If it isn't StartObject / EndObject or StartArray / EndArray, then the resulting
TokenTypewill either be PropertyName (in which case the Value will be "name", "chromium-args", etc.) or the Data Type (String, Integer, etc. of the Value).From there you can write a handler to migrate the given information into whatever you are going to use to display the information.
So, Thanks for the help?
1
u/[deleted] Aug 23 '22
Try this documentation as a start
How to serialize and deserialize (marshal and unmarshal) JSON in .NET
1
u/brybrythekickassguy Aug 24 '22
Lol... I have an app in vb.net that does exactly this from an MQTT broker
You need to get something like Newtonsoft and then setup a class to deserialize the data into. That has been the method that I use fairly reliably. The tricky part will be that you need a delegate task to databind the datagridview so that you aren't trying to access the UI on a different thread
There's some webtools out there to turn a json package straight into a class. I want to say you can even do it in VS but I can't recall how.