Mark
Mark

Reputation: 704

Load Json file in VB.NET/C# using newtonsoft

I Have this kind of JSON file:

[{
    "orderId": "36186",
    "customerID": "584",

    "OrderArticle": [
      {
        "productId": "1780",
        "webCategory": "Cat1",
        "articleQty": "1",
        "perQty": ""
      },
      {
        "productId": "2587",
        "webCategory": "Cat3",
        "articleQty": "58",
        "perQty": ""
      }
    ]
    //..........
}]

and those Classes:

Public Class Response
    Public Property show() As Orders
End Class

Public Class Orders
    Public Property productID As String
    Public Property orderDate As String
    Public Property articles() As New List(Of Orderarticle)
End Class

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

OrderArticle may contain from 1 to x articles.

I'm trying with:

 Dim json As String = My.Computer.FileSystem.ReadAllText(filePath)
 Dim orderList = JsonConvert.DeserializeObject(Of List(Of Response))(json)

But I'm doing something wrong, I'm unable to iterate over articles list in each order. Yes, I've checked all Q/A related with this Issue, The problem is a null reference, not sure if a JSON format problem, a Newtonsoft problem or my fault.

Upvotes: 1

Views: 68

Answers (1)

dbc
dbc

Reputation: 116991

Your data model does not resemble your JSON. Specifically the top-level JSON object has properties "orderId", "customerID" and "OrderArticle" while Response has show and Orders has productID, orderDate and articles. Only the low-level object OrderArticle looks correct.

You need to fix your data model so that the VB.NET and JSON properties correspond -- i.e. have matching names and hierarchy. Of the tools listed in How to auto-generate a C# class file from a JSON string, https://jsonutils.com/ supports VB.NET as well as c# so I auto-generated the following corrected model:

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 Class OrderArticle
    Public Property productId As String
    Public Property webCategory As String
    Public Property articleQty As String
    Public Property perQty As String
End Class

The only alteration I made from the auto-generated model was to change OrderArticle from an array to a list. Then, to iterate over the the articles, do:

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                                

Demo fiddle here.

Upvotes: 2

Related Questions