【发布时间】:2020-12-16 04:57:56
【问题描述】:
我正在使用以下数据模型和代码来异步反序列化某些 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,0,557173022",
"Folder_No" : "G - Detail Drawings",
"Rev_Ver" : "6",
"Cur_Ver" : "6"
}
]
}
【问题讨论】:
-
请花一些时间来正确格式化有问题的代码
-
对不起。肯定会道歉
-
根属性的名称应该是
iDocs_Attribute而不是Children。
标签: .net json stream deserialization system.text.json