I've been searching high and low, and can't find a resolution to this so would welcome some guidance please. Spoiler - working in VB, not C# :-)
I have the following JSON data:
[{
"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"
}]
}]
}]
}
}
}]
Using JSONLINT this validates as valid JSON data.
Trying to parse using Newtonsoft. I have already parsed a downloaded file successfully, and the data above is one of the values sent in an enclosing JSON file, so I know that parse method I have seems to work.
If I use the same method to parse the JSON above, as I did to parse the original download, it fails
Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
Using the following in a second parse method as the first one doesn't work on this. It would seem that I need to approach this particular type of JSON data differently.
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
I've already looked at Newtonsoft.Json: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray and none of them work.
Guidance welcome please, as parsing JSON is completely new to me.
For Each item As JProperty In resultsand then trying to useitemas theJsonReader? I'm not sure about the parsing of theJObject; that looks fine to me. But maybe you want to changeitem.CreateReader()into something likeDim reader As JsonReader = item.CreateReader().