将JSON数组转换为Class对象

时间:2018-10-22 13:35:02

标签: json vb.net

我下面有JSON,我正尝试将其转换为类Utilities

我添加了类Root,以方便JSON数组与Utilities类的列表进行对话。我从尝试过的每个方法中都返回了一个obj,但是字段都为空。正确的方法是什么?

JSON包含在项目(0)中。

  

[{“ jobid”:“ BKTD3G4YOY”,“计划中”:“ 2018-10-16T16:07:28.9963532 + 00:00”,“ routedate”:“ 2018-10-16T17:07:28.0000000 + 01: 00“,”估计“:” 2018-10-16T17:07:28.0000000 + 01:00“,”通知“:true,” ID“:” 3eb41e22-9f68-457e-851b-a97b00e98f6d“,”损失“:23 ,“ breakTimeLoss”:0,“信息”:“游览创建/更新”},{“ jobid”:“ KP8W1XJVZ8”,“计划”:“ 2018-10-16T18:07:28.9963532 + 00:00”,“ routedate “:” 2018-10-16T19:48:45.0000000 + 01:00“,”估计“:” 2018-10-16T19:48:45.0000000 + 01:00“,”通知“:true,” ID“:” 3eb41e22 -9f68-457e-851b-a97b00e98f6d“,”损失“:23,” breakTimeLoss“:0,”信息“:”游览创建/更新“}]

<DataContract()> 
Public Class Utilities
    <DataMember()>
    Public Property jobid As String
        Get
            Return m_scmid
        End Get
        Set(value As String)
            m_scmid = value
        End Set
    End Property
    Private m_scmid As String
    <DataMember()>
    Public Property planned As DateTime
        Get
            Return m_planned
        End Get
        Set(value As DateTime)
            m_planned = value
        End Set
    End Property
    Private m_planned As DateTime
    <DataMember()>
    Public Property routedate As DateTime
        Get
            Return m_routedate
        End Get
        Set(value As DateTime)
            m_routedate = value
        End Set
    End Property
    Private m_routedate As DateTime
    <DataMember()>
    Public Property estimated As DateTime
        Get
            Return m_estmated
        End Get
        Set(value As DateTime)
            m_estmated = value
        End Set
    End Property
    Private m_estmated As DateTime
    <DataMember()>
    Public Property notification As Boolean
        Get
            Return m_notification
        End Get
        Set(value As Boolean)
            m_notification = value
        End Set
    End Property
    Private m_notification As Boolean
    <DataMember()>
    Public Property ID As String
        Get
            Return m_ID
        End Get
        Set(value As String)
            m_ID = value
        End Set
    End Property
    Private m_ID As String
    <DataMember()>
    Public Property source As source
        Get
            Return m_source
        End Get
        Set(value As source)
            m_source = value
        End Set
    End Property
    Private m_source As source
    <DataMember()>
    Public Property loss As Integer
        Get
            Return m_loss
        End Get
        Set(value As Integer)
            m_loss = value
        End Set
    End Property
    Private m_loss As Integer
    Private m_breakTimeLoss As Integer
    <DataMember()>
    Public Property information As String
        Get
            Return m_information
        End Get
        Set(value As String)
            m_information = value
        End Set
    End Property
    Private m_information As String 
End Class 

<DataContract()> 
Public Class source
    <DataMember()>
    Public Property timeStamp As DateTime
        Get
            Return m_timeStamp
        End Get
        Set(value As DateTime)
            m_timeStamp = value
        End Set
    End Property
    Private m_timeStamp As DateTime
    <DataMember()>
    Public Property direction As Integer
        Get
            Return m_direction
        End Get
        Set(value As Integer)
            m_direction = value
        End Set
    End Property
    Private m_direction As Integer 
End Class 

<DataContract()> 
Public Class Root
    <DataMember()>
    Public Property entry As List(Of Utilities)
        Get
            Return m_entry
        End Get
        Set(value As List(Of Utilities))
            m_entry = value
        End Set
    End Property
    Private m_entry As List(Of Utilities) 
End Class

第一个尝试的方法:

 Dim serializer = New DataContractJsonSerializer(GetType(List(Of Root)))
 Dim ms As MemoryStream = New MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(entries(0)))
 Dim obj As List(Of Root) = CType(serializer.ReadObject(ms), List(Of Root))

尝试第二种方法:

 Dim account As List(Of Root) = JsonConvert.DeserializeObject(Of List(Of Root))(entries(0))

感谢您的答复,昨天发表了这个问题之后,我设法使我的代码生效。我确实需要添加设置来处理json中的偏移日期!

Dim jsonSerializerSettings = New JsonSerializerSettings
                jsonSerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat
                jsonSerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset
                jsonSerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind

                Dim obj = JsonConvert.DeserializeObject(Of Root)(entries(0), jsonSerializerSettings)                                              

2 个答案:

答案 0 :(得分:0)

您添加的Root类在这里引起问题,因为JSON中的数组项没有这个额外的层(它们中没有名为entry的属性)。实际上,您根本不需要此包装器类。您应该直接将其反序列化为List(Of Utilities)

Dim list As List(Of Utilities) = JsonConvert.DeserializeObject(Of List(Of Utilities))(entries(0))

顺便说一句,您可以使用Auto-Implemented Properties来简化Utilities类。这将使您不必声明后备字段并为每个属性编写GetSet方法。另外,如果您要使用Json.Net而不是DataContractJsonSerializer,则在这里实际上并不需要DataContractDataMember属性。但是,Json.Net会尊重他们。

另一件事:我注意到您的类缺少breakTimeLoss的公共属性,该属性存在于JSON中,因此如果要捕获该值,则需要添加该属性。相反,您的类具有Source属性,JSON中没有相应的信息。没关系-不会导致任何错误或任何错误-只是想提一下,这似乎是不必要的。

所有这些更改之后,类声明将如下所示:

Public Class Utilities
    Public Property jobid As String
    Public Property planned As DateTime
    Public Property routedate As DateTime
    Public Property estimated As DateTime
    Public Property notification As Boolean
    Public Property ID as String
    Public Property loss As Integer
    Public Property breakTimeLoss As Integer
    Public Property information As String
End Class

正在工作的小提琴:https://dotnetfiddle.net/bfSsts

答案 1 :(得分:0)

您好,下面是您课程的继承人。

   Public Class Utilities

        <JsonProperty("jobid")>
        Public Property Jobid As String

        <JsonProperty("planned")>
        Public Property Planned As DateTime

        <JsonProperty("routedate")>
        Public Property Routedate As DateTime

        <JsonProperty("estimated")>
        Public Property Estimated As DateTime

        <JsonProperty("notification")>
        Public Property Notification As Boolean

        <JsonProperty("ID")>
        Public Property ID As String

        <JsonProperty("loss")>
        Public Property Loss As Integer

        <JsonProperty("breakTimeLoss")>
        Public Property BreakTimeLoss As Integer

        <JsonProperty("information")>
        Public Property Information As String
   End Class