r/visualbasic • • May 05 '22

VB.NET Help Is it possible to seperate one Json-File into different objects?

I have three XamDataGrids, each one has to show different data but from the same json-File. For the first XamDataGrid i set the DataSource to the deserialized object (that's fine, it shows the correct data), but for the other both I just need a snipped of data.

 If OpenFilePath IsNot Nothing Then
        Dim fileReader As StreamReader
        fileReader = My.Computer.FileSystem.OpenTextFileReader(OpenFilePath)
        Dim fileContent As String = fileReader.ReadToEnd
        Dim root = JsonConvert.DeserializeObject(fileContent, GetType(List(Of Artikelstammdaten)))
        dgArticleMasterData.DataSource = CType(root, IEnumerable)
        dgMaterialCosts.DataSource = ??
        dgManufacutringCosts.DataSource = ??

    End If

the json looks like this (i need the data from "Stueckliste" for dgMaterialCosts and "Arbeitsgaenge" for dgManufacturingCosts):

 [
{
	"Artikel": "VAUBEF0010",
	"BezeichnungDE": "Sammelbandantrieb",
	"BezeichnungEN": "Collection Belt Drive N50",
	"Einheit": "STK",
	"MatGrp": "VAU",
	"Kostenart": 1500,
	"Vertriebstext_DE": "Antrieb, Umlenkungen",
	"Vertriebstext_EN": "Drive, Deflections",
	"Stuecklistennummer": "VAUBEF0010",
	"Status": "F",
	"Klasse": "VPTIMV",
	"Mantelflaeche": 1.3,
	"Gewicht": 120.0,
	"KlasseID": "1.2.6.5",
	"Stueckliste": [
		{
			"Verkaufsartikel": "VAUBEF0010",
			"Position": 10,
			"PosArtikel": "Z0306251",
			"PosBezeichnung": "VEL Elektro- Montagematerial",
			"PosKostenart": 9105,
			"Datum": "2022-01-31",
			"Material": 60.51,
			"GMK": 3.63,
			"Lohn": 2.07,
			"Menge": 1,
			"Mengeneinheit": "STK"
		}
	],
	"Arbeitsgaenge": [
		{
			"Verkaufsartikel": "VAUBEF0010",
			"AGNR": 10,
			"Bereich": "Mechanische Montage",
			"Lohn": 89.1,
			"Kostenstelle": 523500,
			"ARBPLATZ": "K950M"
		}
	]
}

]

Changing the json structure is not an option. Thanks for your help'!

1 Upvotes

26 comments sorted by

View all comments

Show parent comment

1

u/Gierschlund96 May 10 '22

Hey, the exception is gone and it works now (kinda). I have to click on "System.Collections.Generic" before the datagrid shows my data (here is a link so you can see what i mean: https://imgur.com/a/HV6NUJE). Is there away to change this? I changed your code a bit. I use "listArtikelstammdaten" instead of "Dim root" for the deserialization:

 If OpenFilePath IsNot Nothing Then
        Dim fileReader As StreamReader
        fileReader = My.Computer.FileSystem.OpenTextFileReader(OpenFilePath)
        Dim fileContent As String = fileReader.ReadToEnd
        listArtikelstammdaten = JsonConvert.DeserializeObject(fileContent, GetType(List(Of Artikelstammdaten)))
        dgArticleMasterData.DataSource = CType(listArtikelstammdaten, IEnumerable)

        Dim stuecklistes = listArtikelstammdaten.Select(Function(r) r.Stueckliste)
        Dim arbeitsgaenge = listArtikelstammdaten.Select(Function(r) r.Arbeitsgaenge)
        dgMaterialCosts.DataSource = stuecklistes
        dgManufacutringCosts.DataSource = arbeitsgaenge

1

u/RJPisscat May 10 '22

I don't know what I'm looking at in that image. I suspect that's a XamDataGrid but I don't know what that is above it, and I don't understand the question. Soz. Perhaps you can rephrase it. Oh wait, that's a XamDataGrid on top with just a Name column and one entry that says System.Collections.Generic? If so, you have to pop the zit to see the XamDG below it?

Different subject:

dgArticleMasterData.DataSource = listArtikelstammdaten

if the same as

dgArticleMasterData.DataSource = CType(listArtikelstammdaten, IEnumerable)

by which I mean, you don't have to cast it, List<T> implements IEnumerable and IEnumerable<T>.

1

u/Gierschlund96 May 10 '22

Oh wait, that's a XamDataGrid on top with just a Name column and one entry that says System.Collections.Generic? If so, you have to pop the zit to see the XamDG below it?

Exactly.

1

u/RJPisscat May 10 '22

This appears on my end it may work, try:

Dim stuecklistes = listArtikelstammdaten.Select(Function(r) r.Stueckliste).ToArray()
Dim arbeitsgaenge = listArtikelstammdaten.Select(Function(r) r.Arbeitsgaenge).ToArray()

2

u/Gierschlund96 May 11 '22

Works. Thanks for your magic as always!

1

u/Gierschlund96 Jun 01 '22

Is there a way to get these as lists? I'm having trouble now because everywhere else I'm working with lists but here with an array. And now i have to connect those arrays with other lists. Already tried ".ToList()" but it doesn't work.

1

u/RJPisscat Jun 01 '22

This may be what you're looking for:

Dim MyList As List(Of Stueckliste) = New List(Of Stueckliste)(listArtikelstammdaten.Select(Function(r) r.Stueckliste))

1

u/Gierschlund96 Jun 01 '22

System.InvalidCastException: "The object of type "WhereSelectListIterator2[ISAAC.VPartManager.Artikelstammdaten,System.Collections.Generic.IList1[ISAAC.VPartManager.Stueckliste]]" cannot be converted to type "System.Collections.Generic.IEnumerable`1[ISAAC.VPartManager.Stueckliste]"."

1

u/RJPisscat Jun 01 '22

I have your Classes, and of course a general idea of what you're doing. What line of code threw that error?

1

u/Gierschlund96 Jun 01 '22

This line threw the error: 'Dim MyList As List(Of Stueckliste) = New List(Of Stueckliste)(listArtikelstammdaten.Select(Function(r) r.Stueckliste))

1

u/RJPisscat Jun 01 '22 edited Jun 01 '22

I realized my simulation was solving an earlier question where you needed arrays of arrays and I don't think it works here.

This is the class structure I generated from your original Json:

Public Class RootObject
    Public Class Rootobject
        Public Property Artikelstammdaten() As Artikelstammdaten
        Public Property Stueckliste() As Stueckliste
        Public Property Arbeitsgänge() As Arbeitsgänge
    End Class
    Public Class Artikelstammdaten
        ' definition goes here
    End Class
    Public Class Stueckliste
        ' definition goes here
    End Class
    Public Class Arbeitsgänge
        ' definition goes here
    End Class
End Class

Is that still correct? If so, when you deserialize, do you get a List(Of RootObject.Rootobject)? Then you create some queries that return List(Of Artikelstammdaten ()) etc? And you want to create an IList of what, every instance of Artikelstammdaten across all instances of RootObject.Rootobject? Or something else?

→ More replies