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/RJPisscat Jun 02 '22

Inside the If: Change allStueckliste back to listStueckliste, which is probably supposed to be empty when you start the inner loop if not the outer loop.

KEEP allStueckliste as the collection in the inner For loop.

1

u/Gierschlund96 Jun 02 '22

Great it works now. Only problem: It only works on the first click. When i click on another row, it doesn't change the datasource

1

u/RJPisscat Jun 02 '22

This is too complicated, I can't respond to that because I'm lost as to what you're doing, and I can't see all the code that is being executed, I can't tell if listStueckliste is empty when you click the button. I don't know where listStueckliste is declared, what is its Type, what else is done with it besides what I've seen in the code snippets you've posted, and I don't know what has changed since a code snippet was posted.

Perhaps set some breakpoints and look at data and see what's doing what where, how you get what you want first time through and why not second time.