在 json 反序列化之后,尽管 json-string 是正确的,但我的对象仍然是空的
After json deserialization my object is still nothing although the json-string is correct
我已经对这个主题做了类似的 post,我应用了他们告诉我的所有更改并尝试了他们的方法,但它仍然没有用(link:Object is nothing after json deserialization)
我想将 json 文件反序列化为 'Artikelstammdaten',然后反序列化为 'Artikelstammdaten' 的列表(还不知道如何迭代反序列化的 json)。这是我的尝试方式:
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
我的 类 看起来像这样:
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
我的 Json- 文件如下所示:
{
"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" 已经有 Json-String 但不知何故它不会反序列化为 "Artikelstammdaten"。我做错了什么?
您的代码正在尝试反序列化 class 的单个实例,因此,如果您仅向它提供文件的一部分,它应该可以工作,例如:
{ "Artikel": "VAUBEF0010" }
您可以通过分配 fileContent
上面的字符串而不是文件内容来测试它 - 它应该可以工作。
要使其反序列化整个文件,您还需要一个 class 来覆盖 json:
的开头
{
"Artikelstammdaten": [
所以你需要:
Public Class Root
Public Property Artikelstammdaten As IList(Of Artikelstammdaten)
End Class
将您的代码修改为:
dim root = JsonConvert.DeserializeObject(Of Root)(fileContent)
所以你做错了什么,第一件事有效:
所以我实际上 运行 你的代码,它抛出一个异常
所以你的模型是错误的你需要修复 属性 为双精度或浮点数
Public Property Gewicht As Double?
之后就可以了
你没有提到异常的事实告诉我你要么忽略了它们,要么你只是写了一个隐藏它们的 try catch。如果存在,请将其删除。异常可以帮助您了解发生了什么。在抛出异常时停止 VS 也很有用,您可以通过选中 window:
中的复选框来实现
我已经对这个主题做了类似的 post,我应用了他们告诉我的所有更改并尝试了他们的方法,但它仍然没有用(link:Object is nothing after json deserialization) 我想将 json 文件反序列化为 'Artikelstammdaten',然后反序列化为 'Artikelstammdaten' 的列表(还不知道如何迭代反序列化的 json)。这是我的尝试方式:
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
我的 类 看起来像这样:
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
我的 Json- 文件如下所示:
{
"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" 已经有 Json-String 但不知何故它不会反序列化为 "Artikelstammdaten"。我做错了什么?
您的代码正在尝试反序列化 class 的单个实例,因此,如果您仅向它提供文件的一部分,它应该可以工作,例如:
{ "Artikel": "VAUBEF0010" }
您可以通过分配 fileContent
上面的字符串而不是文件内容来测试它 - 它应该可以工作。
要使其反序列化整个文件,您还需要一个 class 来覆盖 json:
的开头{
"Artikelstammdaten": [
所以你需要:
Public Class Root
Public Property Artikelstammdaten As IList(Of Artikelstammdaten)
End Class
将您的代码修改为:
dim root = JsonConvert.DeserializeObject(Of Root)(fileContent)
所以你做错了什么,第一件事有效:
所以我实际上 运行 你的代码,它抛出一个异常
所以你的模型是错误的你需要修复 属性 为双精度或浮点数
Public Property Gewicht As Double?
之后就可以了
你没有提到异常的事实告诉我你要么忽略了它们,要么你只是写了一个隐藏它们的 try catch。如果存在,请将其删除。异常可以帮助您了解发生了什么。在抛出异常时停止 VS 也很有用,您可以通过选中 window:
中的复选框来实现