如何在 VB.NET Newtonsoft 中解析 Json 子项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15982358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:14:47  来源:igfitidea点击:

How to Parse Json children in VB.NET Newtonsoft

vb.netjsonjson.net

提问by Charlez

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

我在使用 Newtonsoft Json.Net 库使用 VB.NET 解析 Json 时遇到了麻烦

    Json Data
    ---------
    {
        "CC": "[email protected]",
        "CcFull": [
            {
                "Email": "[email protected]",
                "Name": "John Sample"
            },
            {
                "Email": "[email protected]",
                "Name": "Mike Sample"
            }
        ],
        "FromFull" : {
            "Email": "[email protected]",
            "Name": "John Doe"
         }
    }

I can get a valid JObject thus:

我可以得到一个有效的 JObject,因此:

    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?

然后我可以获取 JToken 列表并遍历它们并轻松获取根项目值 - 但是如何获取 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.

似乎我可以使用 JArray 或解析 item.value - 但语法让我望而却步。

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.

我不想在 VB 中设置一个完整的强类型模型并进行自动反序列化 - 更喜欢在 C# 中执行它的动态方式 - 或者最好只是迭代 CcFull 节点的 n 个子节点并提取电子邮件的值并命名并将它们放在一个通用列表中。

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

似乎在 SO 或谷歌搜索上没有好的 VB.NET 示例。

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

C# 有完全简单的方法来做到这一点 - 但我在这个项目中被困在 VB.NET 中。

Thanks Folks

谢谢大家

采纳答案by Bob Mc

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

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

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 Valuesmethod 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.

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