VbNet · Console
Imports System
Imports System.Linq
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.IO
Imports System.Globalization
Imports System.Reflection
Imports System.ComponentModel.DataAnnotations
Imports System.Xml.Linq
Imports System.Xml
Imports Microsoft.VisualBasic
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports Newtonsoft.Json.Converters
Imports Newtonsoft.Json.Serialization
Imports NUnit.Framework
' https://stackoverflow.com/questions/65095590/load-json-file-in-vb-net-c-using-newtonsoft
Public Class OrderArticle
Public Property productId As String
Public Property webCategory As String
Public Property articleQty As String
Public Property perQty As String
End Class
Public Class Response
Public Property orderId As String
Public Property customerID As String
Public Property OrderArticle As New List(Of OrderArticle) ' Modified from Public Property OrderArticle As OrderArticle()
End Class
Public Module TestClass
Friend Sub Test()
Dim json as String = GetJson()
Dim orderList = JsonConvert.DeserializeObject(Of List(Of Response))(json)
Dim i = 0
For Each response in orderList
For Each article in response.OrderArticle
Console.WriteLine("Article {0}: ", System.Threading.Interlocked.Increment(i))
Console.WriteLine(" productId={0}, webCategory={1}, articleQty={2}, perQty={3}",
article.productId, article.webCategory, article.articleQty, article.perQty)
Next
Next
End Sub
Function GetJson() as String
Dim Jstring As String = <![CDATA[
[{
"orderId": "36186",
"customerID": "584",
"OrderArticle": [
{
"productId": "1780",
"webCategory": "Cat1",
"articleQty": "1",
"perQty": ""
},
{
"productId": "2587",
"webCategory": "Cat3",
"articleQty": "58",
"perQty": ""
}
]
//..........
}]
]]>.Value
Return Jstring
End Function
End Module
Public Module Module1
Public Sub Main()
Console.WriteLine("Environment version: " & Environment.Version.ToString())
Console.WriteLine("Json.NET version: " & GetType(JsonSerializer).Assembly.FullName)
Console.WriteLine("")
Try
TestClass.Test()
Catch ex as Exception
Console.WriteLine("Unhandled exception: ")
Console.WriteLine(ex)
Throw
End Try
End Sub
End Module