问题描述
我正在使用以下数据模型和代码来异步反序列化某些JSON,但是未正确读取根模型的属性。如何解决?
数据模型:
Namespace TestData
Public Class IDocsAttribute
Public Property Project_No As String
Public Property Project_Name As String
Public Property Deliverable As String
Public Property Doc_ID As String
Public Property Doc_No As String
Public Property Title As String
Public Property Doc_Type As String
Public Property Revision As String
Public Property Issue_State As String
Public Property FileName As String
Public Property Supplier_Doc_No As String
Public Property Supplier_Name As String
Public Property Originator_RE_Name As String
Public Property Supplier_Revision As String
Public Property Return_Date As String
Public Property Tag_Equipment_No As String
Public Property Doc_Classification As String
Public Property SDR_Code As String
Public Property URL As String
Public Property Folder_No As String
Public Property Rev_Ver As String
Public Property Cur_Ver As String
Public Property Discipline As String
Public Property Requisition_PO_No As String
End Class
Public Class Root
Public Property Children As List(Of IDocsAttribute)
End Class
End Namespace
反序列化代码:
Private Async Sub IsAvailiable_ClickAsync(sender As Object,e As EventArgs) Handles IsAvailiable.Click
Try
Dim oContent1 = New StringContent("")
Dim response As HttpResponseMessage = Await client.PostAsync("check/",oContent1)
Dim sReponse As String
If (response.IsSuccessStatusCode) Then
sReponse = Await response.Content.ReadAsStringAsync()
Else
sReponse = "Failed"
Exit Sub
End If
Dim squote = Chr(34).ToString()
Dim sbody As String = Requestmessage
Dim content = New StringContent(sbody)
response = Await client.PostAsync("document/",content)
sReponse = Await response.Content.ReadAsStringAsync()
Using stream As IO.Stream = Await response.Content.ReadAsStreamAsync()
Dim filemodules As Root = Await JsonSerializer.DeserializeAsync(Of Root)(stream)
End Using
Catch ex As Exception
End Try
End Sub
以下显示反序列化的JSON格式:
{
"iDocs_Attribute" : [
{
"Project_No" : "236910","Project_Name" : "236910","Deliverable" : "Yes","Doc_ID" : "239585832","Doc_No" : "236910-BAY3-CMC-13101-01-00006","Title" : "E1-E6-22001-01","Doc_Type" : "Supplier Document","Revision" : "1","Issue_State" : "C1-Accepted without Comments","Discipline" : "23.1 - Civil Structural","FileName" : "236910-BAY3-CMC-13101-01-00006.PDF","Supplier_Doc_No" : "1923401907PD-BBS\n-1","Requisition_PO_No" : "CMC-13101-01","Supplier_Name" : "CMC Rebar","Originator_RE_Name" : "Jorge Manrique","Supplier_Revision" : "01","Received_Date" : "11/07/2019","Return_Date" : "11/07/2019","Tag_Equipment_No" : "NO-TAGS","Doc_Classification" : "S2 - Secondary 2","SDR_Code" : "G82 Placing/Marking Drawings and Bar Bending Schedules","URL" : "https://edms-tst.mcdermott.com/edms/redirect/getdocumentf?spec=0,1,239585832,6,557173022","Folder_No" : "G - Detail Drawings","Rev_Ver" : "6","Cur_Ver" : "6"
}
]
}
解决方法
您在这里似乎有两个问题。
首先,您的JSON根属性命名为iDocs_Attribute
而不是Children
:
{
"iDocs_Attribute" : [/* Contents omitted */]
}
因此,相应的vb.net属性必须具有相同的名称:
Public Class Root
Public Property iDocs_Attribute As List(Of IDocsAttribute)
End Class
或者,您可以应用JsonPropertyNameAttribute
来通知序列化器正确的名称:
Public Class Root
<System.Text.Json.Serialization.JsonPropertyName("iDocs_Attribute")> _
Public Property Children As List(Of IDocsAttribute)
End Class
第二,您的反序列化代码从response.Content
两次中读取:
' Reads the response as a string
sReponse = Await response.Content.ReadAsStringAsync()
' Reads the response as a Stream
Using stream As IO.Stream = Await response.Content.ReadAsStreamAsync()
Dim filemodules As Root = Await JsonSerializer.DeserializeAsync(Of Root)(stream)
End Using
我怀疑您应该只做一个,而不能两个都做。如果要使用异步反序列化,请删除对Await response.Content.ReadAsStringAsync()
的第二次调用。