How to make out Json children in VB.NET Newtonsoft

I need to parse Json using VB.NET using the Newtonsoft Json.Net library

Json Data --------- { "CC": " sample.cc@emailDomain.com ", "CcFull": [ { "Email": " sample.cc@emailDomain.com ", "Name": "John Sample" }, { "Email": " another.cc@emailDomain.com ", "Name": "Mike Sample" } ], "FromFull" : { "Email": " myUser@theirDomain.com ", "Name": "John Doe" } } 

I can get a valid JObject this way:

  Dim o As JObject = JObject.Parse(strJson) 

Then I can get a list of JTokens and iterate over them and easily get the values ​​of the root element - but how to get Child entries for CcFull?

  Dim results As List(Of JToken) = o.Children().ToList For Each item As JProperty In results item.CreateReader() Select Case item.Name Case "CC" dim strCC = item.Value.ToString Case "CcFull" 'This has children (Email and Name) End Select Next 

It looks like I could use a JArray or parse item.value - but the syntax eludes me.

I don’t want to configure the whole strongly typed model in VB and do automatic deserialization - prefer more as a dynamic way to do it in C # - or it is advisable to simply iterate over n children for CcFull node and break down enter values ​​for Email and Name and put them in the general list.

There seems to be no good examples of VB.NET in SO or Google.

C # has completely simple ways to do this, but I'm stuck in VB.NET for this project.

Thank you people

+4
source share
1 answer

I am not an expert in implementing Linq for JSON in JSON.Net, but it worked for me.

You are almost everything there. All you have to do is go a little further in the object model.

 Dim results As List(Of JToken) = o.Children().ToList For Each item As JProperty In results item.CreateReader() Select Case item.Name Case "CC" Dim strCC = item.Value.ToString Case "CcFull" Dim strEmail As String Dim strName As String For Each subitem As JObject In item.Values strEmail = subitem("Email") strName = subitem("Name") Next End Select Next 

The item that you get from the list of results contains subheadings, as you noted. This subtitle has a number of meanings - an array indicated by brackets in your JSON string. This Values method is IEnumerable, so we can iterate over it, getting a JObject from each iteration. This object is the only entry in the CcFull array. You can then use the property name as an index to get the value for this property.

+4
source

All Articles