How to Parse Json children in VB.NET Newtonsoft

前端 未结 1 1669
谎友^
谎友^ 2020-12-19 10:20

I am having touble parsing Json using VB.NET using the Newtonsoft Json.Net library

    Json Data
    ---------
    {
        \"CC\": \"sample.cc@emailDomain.         


        
相关标签:
1条回答
  • 2020-12-19 10:46

    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.

    0 讨论(0)
提交回复
热议问题