如何创建一个类以在vb.net中序列化recreation.gov营地json数据

huangapple go评论56阅读模式
英文:

How to create a class to serialize recreation.gov campsite json data in vb.net

问题

struct RootObject: Codable {
    let campsites: Campsites
    let count: Int
}

struct Campsites: Codable {
    let _1: Campsite
    let _2: Campsite
}

struct Campsite: Codable {
    let availabilities: Availabilities
    let campsite_id: String
    let campsite_reserve_type: String
    let campsite_rules: String? // Replace 'Object' with the correct type if possible
    let campsite_type: String
    let capacity_rating: String
    let loop: String
    let max_num_people: Int
    let min_num_people: Int
    let quantities: Quantities
    let site: String
    let supplemental_camping: String? // Replace 'Object' with the correct type if possible
    let type_of_use: String
}

struct Availabilities: Codable {
    let _20230801T000000Z: String
    let _2023080T000000Z: String
}

struct Quantities: Codable {
    let _20230801T000000Z: Int
    let _20230831T000000Z: Int
}
英文:

I am trying to use the recreations.gov api to find available campsites. When I receive the data I am not sure how to serialize it. Can someone help me create the correct class structure? I removed some of the availability and quantity entries. One for each day of the month is returned.

Thanks,
Jeremiah

{
  "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
}

I have read tons of post about json serialization and tried creating the class with https://jsonutils.com/ but the values become properties of the class which doesn't make sense. I am wondering if the json they return is invalid.

   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

答案1

得分: 0

这个解决方案需要一些手动操作。

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)
        ' 找到营地集合
        Dim campsitesjson = token.SelectToken("campsites")
        Dim recSite As New RecreationSite

        Dim campcounter As Integer = 0

        ' 循环遍历集合
        For Each obj In campsitesjson

            campcounter += 1
            ' 找到特定的营地
            Dim campSiteToken = token.SelectToken($"campsites.{campcounter}")
            Dim campSiteItem As Campsite = JsonConvert.DeserializeObject(Of Campsite)(campSiteToken.ToString)

            ' 找到可用性列表
            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

            ' 找到数量列表
            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
            ' 将营地添加到集合
            recSite.CampSiteLists.Add(campSiteItem)

        Next

    End Sub
英文:

This solution will get you there with a bit of manual effort.

The json class:

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

A method to parse the incoming json and leave you with a collection of campsites:

    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

huangapple
  • 本文由 发表于 2023年5月17日 23:50:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定