如何在VB.NET Newtonsoft中解析Json子级 [英] How to Parse Json children in VB.NET Newtonsoft

查看:141
本文介绍了如何在VB.NET Newtonsoft中解析Json子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Newtonsoft Json.Net库使用VB.NET解析Json

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"
         }
    }

我可以这样获得一个有效的JObject:

I can get a valid JObject thus:

    Dim o As JObject = JObject.Parse(strJson)

然后我可以获取JTokens的列表并进行遍历,并轻松获取根项值-但是如何获取CcFull的Child记录?

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 

似乎我可以使用JArray或解析item.value了-但是语法使我难以理解.

It seems like I might be able to use a JArray or parse the item.value - but the syntax eludes me.

我不想在VB中设置整个强类型模型并进行自动反序列化-更喜欢在C#中使用动态方式进行建模-或最好只为CcFull节点遍历n个子代并选择电子邮件"和名称"的值,并将它们放在通用列表中.

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.

似乎在SO或Googling上没有很好的VB.NET示例.

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

C#具有完全简单的方法来执行此操作-但是我在VB.NET中陷于该项目.

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

感谢民间

推荐答案

我不是JSON.Net中Linq to JSON实现的专家,但这对我有用.

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

从结果列表中获得的项目具有子项目,如您所指出的.该子项具有一系列值-由JSON字符串中的方括号表示的数组.该Values方法是一个IEnumerable,因此我们可以对其进行迭代,从每次迭代中接收一个JObject.该对象表示CcFull数组中的单个条目.然后,您可以使用属性名称作为索引来检索该属性的值.

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.

这篇关于如何在VB.NET Newtonsoft中解析Json子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆