Search code examples
jsonvb.netserializationjson.netdeserialization

After json deserialization my object is still nothing although the json-string is correct


I already made a similar post about this topic, i applied all the changes they told me and tried their approaches, but it still didnt work (link: Object is nothing after json deserialization) I want to deserialize a json file into 'Artikelstammdaten' and afterwards to a list of 'Artikelstammdaten' (don't know how to iterate over the deserialized json yet). Here is how i tried it:

        If OpenFilePath IsNot Nothing Then
        Dim fileReader As StreamReader
        fileReader = My.Computer.FileSystem.OpenTextFileReader(OpenFilePath)
        Dim fileContent As String = fileReader.ReadToEnd
        Dim artikelstammdaten As New Artikelstammdaten
        artikelstammdaten = JsonConvert.DeserializeObject(Of Artikelstammdaten)(fileContent)
    End If

My classes look like this:

Public Class Artikelstammdaten

Public Property Artikel As String
Public Property BezeichnungDE As String
Public Property BezeichnungEN As String
Public Property Einheit As String
Public Property MatGrp As String
Public Property Kostenart As Integer?
Public Property Vertriebstext_DE As String
Public Property Vertriebstext_EN As String
Public Property Stuecklistennummer As String
Public Property Status As String
Public Property Klasse As String
Public Property Mantelflaeche As Double?
Public Property Gewicht As Integer?
Public Property KlasseID As String
Public Property Stueckliste As IList(Of Stueckliste)
Public Property Arbeitsgaenge As IList(Of Arbeitsgaenge)


End Class

Public Class Stueckliste
Public Property Verkaufsartikel As String
Public Property Position As Integer?
Public Property PosArtikel As String
Public Property PosBezeichnung As String
Public Property PosKostenart As Integer?
Public Property Datum As String
Public Property Material As Double?
Public Property GMK As Double?
Public Property Lohn As Double?
Public Property Menge As Integer?
Public Property Mengeneinheit As String
End Class

Public Class Arbeitsgaenge

Public Property Verkaufsartikel As String
Public Property AGNR As Integer?
Public Property Bereich As String
Public Property Lohn As Double?
Public Property Kostenstelle As Integer?
Public Property ARBPLATZ As String
End Class

My Json-File looks like this:

{
  "Artikelstammdaten": [
    { "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": "VTPIMV" },
    { "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"
        }
       ]
    }
  ]
}

"fileContent" already has the Json-String but somehow it won't deserialize into "Artikelstammdaten". What am i doing wrong?


Solution

  • your code is trying to deserialize a single instance of a class, so as is, it should work if you feed it only part of the file like:

    { "Artikel": "VAUBEF0010" }
    

    you can test this by assigning fileContent the above string instead of file content -it should work.

    to make it deserialize the full file you need one more class to cover the beginning of your json:

    {
      "Artikelstammdaten": [
    

    so you need:

    Public Class Root 
        Public Property Artikelstammdaten As IList(Of Artikelstammdaten)
    End Class
    

    than modify your code to:

    dim root = JsonConvert.DeserializeObject(Of Root)(fileContent)
    

    so you are doing something wrong, the first thing works:

    enter image description here

    so I actually run your code and it throws an exception enter image description here

    so your model is wrong you need to fix that property to be double or float

    Public Property Gewicht As Double?
    

    after that it works

    enter image description here

    the fact that you did not mention an exception tells my you either are ignoring them or you just have written a try catch that hides them. Remove that if it exists. Exceptions are there to help you know what is going on. Also it is useful to stop VS on exceptions at the line they are thrown you can make that by checking the checkbox in this window:

    enter image description here