VB.Net JSON解析-从JsonReader阅读JObject时出错,当前JsonReader项不是对象

ghhaqwfi  于 2023-05-08  发布在  .NET
关注(0)|答案(1)|浏览(284)

我一直在寻找高和低,并不能找到一个解决方案,所以会欢迎一些指导,请。Spoiler -在VB中工作,而不是C#:-)
下面是JSON数据:

[{
    "payload": {
        "MainBlock": {
            "S0": {
                "interfaceId": "IF-XXX",
                "schemaVersion": "1.1.1",
                "eventCode": "[ABCPeriodData]"
            },
            "S1": {
                "environment": "DEV",
                "senderUniqueReference": "S-005-1234567abc-SUP-20222313-12345687A",
                "sentTimestamp": "2022-12-31T12:35:45\u002B06:30",
                "senderId": "4ce0a6cd4b",
                "senderRole": "ABC",
                "DIPConnectionProviderId": "1009012345"
            },
            "A0": {
                "always": ["SUP", "SDS", "ADS", "UMDS"]
            },
            "D0": {
                "transactionId": "T-022-4ce0a6cd4b-ABC-20230503-000404",
                "transactionTimestamp": "2023-05-03T13:34:34\u002B00:00",
                "publicationId": "PUB-XXX"
            }
        },
        "SecondBlock": {
            "B903List": [{
                "calculationDay": {
                    "calculationDayDate": "2022-12-31T12:35:45\u002B06:30",
                    "calculationPeriodDuration": 30
                },
                "XYZcalculationPeriods": [{
                    "XYZroup": {
                        "GSPGroupID": "_K",
                        "connectionTypeIndicator": "W",
                        "domesticPremiseIndicator": true,
                        "marketSegmentIndicator": "A",
                        "measurementQuantityID": "AI"
                    },
                    "XYZroupcalculationPeriods": [{
                        "calculationPeriodEffectiveToDateTime": "2022-12-31T12:35:45\u002B06:30",
                        "loadShapePeriodValue": "123456789.123",
                        "defaultLoadShapeFlag": "A"
                    }, {
                        "calculationPeriodEffectiveToDateTime": "2022-12-31T12:35:45\u002B06:30",
                        "loadShapePeriodValue": "123456789.123",
                        "defaultLoadShapeFlag": "A"
                    }]
                }]
            }]
        }
    }
}]

使用JSONLINT验证是否为有效的JSON数据。
尝试使用Newtonsoft进行解析。我已经成功解析了一个下载的文件,上面的数据是在一个封闭的JSON文件中发送的值之一,所以我知道我的解析方法似乎可以工作。
如果我使用与解析原始下载相同的方法来解析上面的JSON,它会失败

Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.

在第二个解析方法中使用下面的代码作为第一个解析方法是不起作用的。看起来我需要以不同的方式处理这种特定类型的JSON数据。

Dim o As JObject = JObject.Parse(Json)
            Dim results As List(Of JToken) = o.Children().ToList
            For Each item As JProperty In results
                item.CreateReader()
                If item.Value.Type = JTokenType.Array Then
                    Dim results2 As List(Of JToken) = item.Value.ToList
                    For Each subitem As JObject In results2
                        WorkingDictionary = New Dictionary(Of String, String)
                        Dim results3 As List(Of JToken) = subitem.Children().ToList
                        For Each temp2 As JProperty In results3
                            temp2.CreateReader()
                            Try
                                Debug.Print(temp2.Name)

                                WorkingDictionary.Add(temp2.Name, temp2.Value.ToString)

                                'Debug.Print(temp2.Name)
                                'Debug.Print(temp2.Value.ToString)
                            Catch ex As Exception
                                Debug.Print(ex.StackTrace)
                                Debug.Print(ex.Message)
                            End Try

                        Next
                        RetVal.Add(WorkingDictionary)
                    Next
                Else
                    Debug.Print(item.Name.ToString + "   " + item.Value.ToString)
                End If
            Next
            '         Next
        Catch ex As Exception
            Debug.Print(ex.StackTrace)
            Debug.Print(ex.Message)
        End Try

我已经看过Newtonsoft.Json: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray了,它们都不起作用。
欢迎指导,因为解析JSON对我来说是全新的。

nxowjjhe

nxowjjhe1#

有一个以前的帖子从Serge,我打算标记为答案,但它已被删除。
代码部分应该以

Dim o As JArray = JArray.Parse(Json)

然后,这是根据需要进一步解析的基础。
如果@serge想转发他们最初的评论,我很乐意标记为接受的答案。

相关问题