Solved

LINQ query on json sub array

Posted on 2016-09-15
2
45 Views
Last Modified: 2016-09-18
I am working on the attached jSon response. I need to read the sub-array named "details" and populate a datagrid.
I am using VB.NET.

Attached is how I declared the classes to be used in my LINQ query.

Below is my code so far.

Dim docs = JArray.FromObject(parsedObject("invoice")).Select(Function(x) x.ToObject(Of MyClass.InvoiceList)())
'Dim invoiceL = docs.Select(Function(x) x).OrderBy(Function(x) x.number).ToList

Dim InvoiceDetailL = docs.Select(Function(g) New With {g.details.First().totalBoxes, g.details.First().description, g.details.First().store, g.details.First().productId, g.details.First().totalPrice, g.details.First().Type}).ToList()
           
            gdviewDetail.DataSource = InvoiceDetailL
            gdviewDetail.DataBind()

As you can see, I am able to create "InvoiceDetailL" with only the first row of "details", as I used the function First(). How can I obtain all elements in the sequence?
Class_Declaration_EE.txt
jSon_Response_EE.txt
0
Comment
Question by:Angel02
2 Comments
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
First of all, your class structure does not match the presented json.  If I go by the structure you have laid out, then this would be your class structure:
Public Class [MyClass]
	Public Property status As String
	Public Property invoice As List(Of InvoiceList)
End Class

Public Class InvoiceList
	Public Property id As Integer
	Public Property number As Integer
	Public Property customerName As String
	Public Property customerId As Integer
	Public Property details As New List(Of InvoiceDetailList)
End Class

Public Class InvoiceDetailList
	Public Property store As String
	Public Property productId As Integer
	Public Property Description As String
	Public Property totalBoxes As Integer
	Public Property Type As String
	Public Property totalPrice As Double
End Class

Open in new window

Now when deserialized into this class structure, we would use First to get the first InvoiceList item, then set the details to the datasource of the DataGridView; e.g. -
gdviewDetail.DataSource = docs.invoice.First().details
gdviewDetail.DataBind()

Open in new window


Proof of concept -
Imports Newtonsoft.Json.Linq
Imports Newtonsoft.Json

Public Class Form1
	Const json = "{""status"": ""OK"",""invoice"": [{""id"": 45667,""number"": ""2344"",""customerName"": ""ABC"",""customerId"": 676,""details"": [{""store"": ""XY"",""Type"": ""2"",""description"": ""PQR"",""productId"": 333,""totalBoxes"": 10,""totalPrice"": ""30.00""},{""store"": ""YZ"",""Type"": ""2"",""description"": ""STU"",""productId"": 222,""totalBoxes"": 5,""totalPrice"": ""15.00""},{""store"": ""ZA"",""Type"": ""1"",""description"": ""EGF"",""productId"": 332,""totalBoxes"": 20,""totalPrice"": ""50.00""}]}]}"

	Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
		Dim docs = JsonConvert.DeserializeObject(Of [MyClass])(json)
		DataGridView1.DataSource = docs.invoice.First().details
	End Sub
End Class

Public Class [MyClass]
	Public Property status As String
	Public Property invoice As List(Of InvoiceList)
End Class

Public Class InvoiceList
	Public Property id As Integer
	Public Property number As Integer
	Public Property customerName As String
	Public Property customerId As Integer
	Public Property details As New List(Of InvoiceDetailList)
End Class

Public Class InvoiceDetailList
	Public Property store As String
	Public Property productId As Integer
	Public Property Description As String
	Public Property totalBoxes As Integer
	Public Property Type As String
	Public Property totalPrice As Double
End Class

Open in new window


Produces the following output -Capture.JPG
-saige-
0
 

Author Comment

by:Angel02
Comment Utility
Thank you it_saige.

With your idea, I did the following

Dim InvoiceDetailL = docs.First().details.Select(Function(g) New With {g.totalBoxes, g.description, g.store, g.productId, g.totalPrice, g.Type}).ToList()
gdviewDetail.DataSource = InvoiceDetailL

That worked!

Hope it's a good way.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

We will take a look at the d3.js library for visualizations.  I will provide a walkthrough of a short bar graph example as well as introduce you to the d3.js API.  We also will explore links of other examples and further information regarding SVG's.
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now