如何创建一个类来序列化recreation.gov中的www.example.com露营地JSON数据vb.net

hyrbngr7  于 2023-05-19  发布在  Go
关注(0)|答案(1)|浏览(167)

我正在尝试使用recreations.gov API来查找可用的露营地。当我收到数据时,我不知道如何序列化它。有人能帮我创建正确的类结构吗?我删除了一些可用性和数量条目。每月的每一天都返回一个。
谢了,耶利米

{
  "campsites": {
    "1": {
      "availabilities": {
        "2023-08-01T00:00:00Z": "Reserved",
        "2023-08-0T00:00:00Z": "Reserved"
      },
      "campsite_id": "1",
      "campsite_reserve_type": "Site-Specific",
      "campsite_rules": null,
      "campsite_type": "RV NONELECTRIC",
      "capacity_rating": "Single",
      "loop": "C ",
      "max_num_people": 6,
      "min_num_people": 0,
      "quantities": {
        "2023-08-01T00:00:00Z": 0,
        "2023-08-31T00:00:00Z": 0
      },
      "site": "065",
      "supplemental_camping": null,
      "type_of_use": "Overnight"
    },
    "2": {
      "availabilities": {
        "2023-08-01T00:00:00Z": "Reserved",
        "2023-08-31T00:00:00Z": "Reserved"
      },
      "campsite_id": "2",
      "campsite_reserve_type": "Site-Specific",
      "campsite_rules": null,
      "campsite_type": "RV NONELECTRIC",
      "capacity_rating": "Single",
      "loop": "C",
      "max_num_people": 6,
      "min_num_people": 0,
      "quantities": {
        "2023-08-01T00:00:00Z": 0,
        "2023-08-31T00:00:00Z": 0
      },
      "site": "066",
      "supplemental_camping": null,
      "type_of_use": "Overnight"
    }
  },
  "count": 99
}

我读了大量关于json序列化的文章,并尝试用https://jsonutils.com/创建类,但值变成了类的属性,这毫无意义。我想知道他们返回的JSON是否无效。

Public Class Rootobject
        Public Property campsites As Campsites
        Public Property count As Integer
    End Class

    Public Class Campsites
        Public Property _1 As _1
        Public Property _2 As _2
    End Class

    Public Class _1
        Public Property availabilities As Availabilities
        Public Property campsite_id As String
        Public Property campsite_reserve_type As String
        Public Property campsite_rules As Object
        Public Property campsite_type As String
        Public Property capacity_rating As String
        Public Property _loop As String
        Public Property max_num_people As Integer
        Public Property min_num_people As Integer
        Public Property quantities As Quantities
        Public Property site As String
        Public Property supplemental_camping As Object
        Public Property type_of_use As String
    End Class

    Public Class Availabilities
        Public Property _20230801T000000Z As String
        Public Property _2023080T000000Z As String
    End Class

    Public Class Quantities
        Public Property _20230801T000000Z As Integer
        Public Property _20230831T000000Z As Integer
    End Class

    Public Class _2
        Public Property availabilities As Availabilities1
        Public Property campsite_id As String
        Public Property campsite_reserve_type As String
        Public Property campsite_rules As Object
        Public Property campsite_type As String
        Public Property capacity_rating As String
        Public Property _loop As String
        Public Property max_num_people As Integer
        Public Property min_num_people As Integer
        Public Property quantities As Quantities1
        Public Property site As String
        Public Property supplemental_camping As Object
        Public Property type_of_use As String
    End Class

    Public Class Availabilities1
        Public Property _20230801T000000Z As String
        Public Property _20230831T000000Z As String
    End Class

    Public Class Quantities1
        Public Property _20230801T000000Z As Integer
        Public Property _20230831T000000Z As Integer
    End Class
yhived7q

yhived7q1#

这个解决方案只需要一点手工操作就可以实现。
JSON类:

Imports Newtonsoft.Json

Public Class CampSiteProcessor

    Public Class RecreationSite

        Public Sub New()
            CampSiteLists = New List(Of Campsite)
        End Sub
        Public Property CampSiteLists As List(Of Campsite)
        Public Property campsites As String
        Public Property count As Integer
    End Class

    Public Class Campsite

        Public Sub New()
            AvailObjects = New List(Of AvailObject)
            QuantityObjects = New List(Of QuantityObject)
        End Sub
        Public Property AvailObjects As IList(Of AvailObject)
        Public Property campsite_id As String
        Public Property campsite_reserve_type As String
        Public Property campsite_rules As Object
        Public Property campsite_type As String
        Public Property capacity_rating As String

        <JsonProperty(PropertyName:="loop")>
        Public Property _loop As String
        Public Property max_num_people As Integer
        Public Property min_num_people As Integer
        Public Property QuantityObjects As IList(Of QuantityObject)
        Public Property site As String
        Public Property supplemental_camping As Object
        Public Property type_of_use As String
    End Class

    Public Class AvailObject
        Public Property DateValue As String
        Public Property StringValue As String
    End Class

    Public Class QuantityObject
        Public Property DateValue As String
        Public Property AmountValue As String
    End Class

End Class

一个解析传入json的方法,并给你留下一个露营地的集合:

Private Sub ParseCampSites(jsonString As String)

        Dim token As JToken = JObject.Parse(jsonString)
        ' find the campsites collection
        Dim campsitesjson = token.SelectToken("campsites")
        Dim recSite As New RecreationSite

        Dim campcounter As Integer = 0

        ' loop through the collection
        For Each obj In campsitesjson

            campcounter += 1
            ' find the specific campsite
            Dim campSiteToken = token.SelectToken($"campsites.{campcounter}")
            Dim campSiteItem As Campsite = JsonConvert.DeserializeObject(Of Campsite)(campSiteToken.ToString)

            ' find the availabilities list
            Dim availabilities = token.SelectToken($"campsites.{campcounter}.availabilities")
            For Each item As Object In availabilities
                Dim dateValue As String = item.name
                Dim statusValue As String = item.value
                campSiteItem.AvailObjects.Add(New AvailObject With {.DateValue = dateValue, .StringValue = statusValue})
            Next

            ' find the quantities list
            Dim quantities = token.SelectToken($"campsites.{campcounter}.quantities")
            For Each item As Object In quantities
                Dim dateValue As String = item.name
                Dim amountValue As String = item.value
                campSiteItem.QuantityObjects.Add(New QuantityObject With {.DateValue = dateValue, .AmountValue = amountValue})
            Next
            ' add campsite to collection
            recSite.CampSiteLists.Add(campSiteItem)

        Next

    End Sub

相关问题