How to Parse Json children in VB.NET Newtonsoft

偶尔善良 提交于 2019-12-29 07:46:30

问题


I am having touble parsing 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 thus:

    Dim o As JObject = JObject.Parse(strJson)

Then I can get list of a JTokens and iterate through them and easily get the root item values - but how get the Child records 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 seems like I might be able to use a JArray or parse the item.value - but the syntax eludes me.

I don't want to setup a whole strongly typed model in VB and do an automatic deserialze - prefer more like the Dynamic way of doing it in C# - or preferably just iterate over n children for the CcFull node and pluck out the values for Email and Name and put them in a generic list.

Seems there are no good VB.NET examples on SO or by Googling.

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

Thanks Folks


回答1:


I'm no expert on the Linq to JSON implementation in JSON.Net, but this worked for me.

You're pretty much all the way there. All you need to do is drill down 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 results list has sub-items, as you noted. That sub item has a series of values - the array denoted by the brackets in your JSON string. That Values method is an IEnumerable so we can iterate over it, receiving a JObject from each iteration. That object represents a single entry in the CcFull array. You can then use the property name as an index to retrieve the value for that property.



来源:https://stackoverflow.com/questions/15982358/how-to-parse-json-children-in-vb-net-newtonsoft

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!