System.Text.JSON.DeserializeAsync JSON 显示的对象我的代码没有正常发生

System.Text.JSON.DeserializeAsync JSON Objects as shown my code is not happening properly

我正在使用以下数据模型和代码异步反序列化一些 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"
    }
  ]
}

你这里好像有两个问题。

首先,你的根 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.Contenttwice:

读取
' 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().

的第二次调用