使用JsonConvert.Deserialize时,变量显示“无”

问题描述 投票:0回答:1

使用JsonConvert.DeserializeObject时,我遇到变量显示“无”的问题。我搜索了其他论坛,每个人似乎建议使用下面的代码snippit,但每次我实现和测试值显示为“无”。任何人都可以指出我正在做的正确方向导致这个问题吗?

Dim userid As Workers.Person = JsonConvert.DeserializeObject(Of Workers.Person)(str)

班级结构如下。修剪以适应,省略某些类型和数据。

{
  "workers": [
    {
      "associateOID": "G4O73G9Z62SL2NFM",
      "workerID": {
        "idValue": "1000199",
        "schemeCode": {
          "codeValue": "EID",
          "shortName": "Employee ID"
        }
      },
      "person": {
        "birthDate": "1972-01-27",
        "genderCode": {
          "codeValue": "M",
          "shortName": "Male"
        },
        "maritalStatusCode": {
          "effectiveDate": "2010-07-04",
          "codeValue": "M",
          "shortName": "Married"
        },
        "socialInsurancePrograms": [
          {
            "itemID": "1",
            "nameCode": {
              "shortName": "Medicare"
            },
            "countryCode": "US",
            "coveredIndicator": true
          }
        ],
        "immigrationDocuments": [
          {
            "documentID": "PR-000018US",
            "countryCode": "CA",
            "typeCode": {
              "codeValue": "PR",
              "shortName": "Permanent Resident"
            },
            "issuingParty": {
              "nameCode": {
                "codeValue": "CIC",
                "shortName": "Office of Citizenship and Immigration"
              },
              "address": {
                "lineOne": "365 Laurier Avenue West",
                "cityName": "Ottawa",
                "countrySubdivisionLevel1": {
                  "subdivisionType": "province",
                  "codeValue": "ON",
                  "shortName": "Ontario"
                },
                "countryCode": "CA",
                "postalCode": "K1A 1L1"
              }
            },
            "issueDate": "2013-04-15",
            "expirationDate": "2018-04-14",
            "authorizedStayDuration": "P5Y0M0DT0H0M0S",
            "reentryRequirementDuration": "P1Y0M0DT0H0M0S"
          }
        ],
        ...More Properties...
        }
      },
      "workerDates": {
        "originalHireDate": "2002-08-12"
      },
      "workerStatus": {
        "statusCode": {
          "codeValue": "A",
          "shortName": "Active"
        },
        "reasonCode": {
          "codeValue": "NH",
          "shortName": "New Hire"
        }
      },
      "photos": [
        {
          "itemID": "1",
          "nameCode": {
            "shortName": "portal pic"
          },
          "links": [
            {
              "href": "/hr/v1/worker/G4O73G9Z62SL2NFM/photoUri/A1316C0D49011201.jpg",
              "rel": "/adp/image"
            }
          ]
        }
      ],
      "businessCommunication": {
        "landlines": [
          {
            "itemID": "1",
            "countryDialing": "1",
            "areaDialing": "973",
            "dialNumber": "5553245",
            "formattedNumber": "1 (973) 555-3245"
          }
        ],
        "mobiles": [
          {
            "itemID": "1",
            "countryDialing": "1",
            "areaDialing": "973",
            "dialNumber": "7133851",
            "formattedNumber": "1 (973) 713-3851"
          }
        ],
        "faxes": [
          {
            "itemID": "1",
            "countryDialing": "1",
            "areaDialing": "973",
            "dialNumber": "123000",
            "formattedNumber": "1 (973) 123-000"
          }
        ],
        "emails": [
          {
            "itemID": "1",
            "emailUri": "[email protected]"
          }
        ],
        "internetAddresses": [
          {
            "itemID": "1",
            "uri": "http://www.adpcoffee.com/AnthonyAlbright"
          }
        ],
        "instantMessages": [
          {
            "itemID": "1",
            "uri": "[email protected]"
          }
        ]
      },
      "workAssignments": [],
      "links": [
        {
          "href": "/hr/v2/workers/G4O73G9Z62SL2NFM",
          "rel": "self"
        }
      ]
    }
  ]
}

类示例:

Public Class Workers

Public Class SchemeCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class WorkerID
    Public Property idValue As String
    Public Property schemeCode As SchemeCode
End Class

Public Class GenderCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class MaritalStatusCode
    Public Property effectiveDate As String
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class NameCode
    Public Property shortName As String
End Class

Public Class SocialInsuranceProgram
    Public Property itemID As String
    Public Property nameCode As NameCode
    Public Property countryCode As String
    Public Property coveredIndicator As Boolean
End Class

Public Class TypeCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class NameCode2
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class CountrySubdivisionLevel1
    Public Property subdivisionType As String
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class Address
    Public Property lineOne As String
    Public Property cityName As String
    Public Property countrySubdivisionLevel1 As CountrySubdivisionLevel1
    Public Property countryCode As String
    Public Property postalCode As String
End Class

Public Class IssuingParty
    Public Property nameCode As NameCode2
    Public Property address As Address
End Class

Public Class NameCode4
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class Address3
    Public Property attentionOfName As String
    Public Property lineOne As String
    Public Property cityName As String
    Public Property countrySubdivisionLevel1 As CountrySubdivisionLevel13
    Public Property countryCode As String
    Public Property postalCode As String
End Class

Public Class IssuingParty3
    Public Property nameCode As NameCode4
    Public Property address As Address3
End Class

Public Class WorkAuthorizationDocument
    Public Property documentID As String
    Public Property countryCode As String
    Public Property typeCode As TypeCode3
    Public Property issuingParty As IssuingParty3
    Public Property issueDate As String
    Public Property expirationDate As String
End Class

Public Class PreferredName
    Public Property nickName As String
    Public Property familyName1 As String
    Public Property formattedName As String
End Class

Public Class FormerName
    Public Property givenName As String
    Public Property familyName1 As String
    Public Property formattedName As String
End Class

Public Class BirthName
    Public Property givenName As String
    Public Property familyName1 As String
    Public Property formattedName As String
End Class

Public Class CountrySubdivisionLevel14
    Public Property subdivisionType As String
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class BirthPlace
    Public Property formattedBirthPlace As String
    Public Property cityName As String
    Public Property countrySubdivisionLevel1 As CountrySubdivisionLevel14
    Public Property postalCode As String
    Public Property countryCode As String
End Class

Public Class NameCode5
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class CountrySubdivisionLevel15
    Public Property subdivisionType As String
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class OtherPersonalAddress
    Public Property nameCode As NameCode5
    Public Property lineOne As String
    Public Property cityName As String
    Public Property countrySubdivisionLevel1 As CountrySubdivisionLevel15
    Public Property countryCode As String
    Public Property postalCode As String
End Class

Public Class IdentificationMethodCode
    Public Property codeValue As String
End Class

Public Class RaceCode
    Public Property identificationMethodCode As IdentificationMethodCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class CitizenshipCountryCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class ResidencyCountryCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class MilitaryStatusCode
    Public Property effectiveDate As String
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class MilitaryClassificationCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class NameCode6
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class GovernmentID
    Public Property itemID As String
    Public Property idValue As String
    Public Property nameCode As NameCode6
    Public Property countryCode As String
End Class

Public Class SalutationCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class TypeCode4
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class PreferredSalutation
    Public Property salutationCode As SalutationCode
    Public Property typeCode As TypeCode4
    Public Property sequenceNumber As Integer
End Class

Public Class LegalName
    Public Property preferredSalutations As List(Of PreferredSalutation)
    Public Property givenName As String
    Public Property familyName1 As String
    Public Property formattedName As String
End Class

Public Class NameCode7
    Public Property shortName As String
End Class

Public Class CountrySubdivisionLevel16
    Public Property subdivisionType As String
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class GeoCoordinate
    Public Property latitude As Double
    Public Property longitude As Double
End Class

Public Class LegalAddress
    Public Property nameCode As NameCode7
    Public Property lineOne As String
    Public Property cityName As String
    Public Property countrySubdivisionLevel1 As CountrySubdivisionLevel16
    Public Property countryCode As String
    Public Property postalCode As String
    Public Property geoCoordinate As GeoCoordinate
End Class

Public Class NameCode8
    Public Property shortName As String
End Class

Public Class Landline
    Public Property itemID As String
    Public Property nameCode As NameCode8
    Public Property countryDialing As String
    Public Property areaDialing As String
    Public Property dialNumber As String
    Public Property formattedNumber As String
End Class

Public Class NameCode9
    Public Property shortName As String
End Class

Public Class Mobile
    Public Property itemID As String
    Public Property nameCode As NameCode9
    Public Property countryDialing As String
    Public Property areaDialing As String
    Public Property dialNumber As String
    Public Property formattedNumber As String
End Class

Public Class NameCode10
    Public Property shortName As String
End Class

Public Class Email
    Public Property itemID As String
    Public Property nameCode As NameCode10
    Public Property emailUri As String
End Class

Public Class SocialNetwork
    Public Property itemID As String
    Public Property uri As String
End Class

Public Class Communication
    Public Property landlines As List(Of Landline)
    Public Property mobiles As List(Of Mobile)
    Public Property emails As List(Of Email)
    Public Property socialNetworks As List(Of SocialNetwork)
End Class

Public Class Person
    Public Property birthDate As String
    Public Property genderCode As GenderCode
    Public Property maritalStatusCode As MaritalStatusCode
    Public Property socialInsurancePrograms As List(Of SocialInsuranceProgram)
    Public Property immigrationDocuments As List(Of ImmigrationDocument)
    Public Property passports As List(Of Passport)
    Public Property workAuthorizationDocuments As List(Of WorkAuthorizationDocument)
    Public Property preferredName As PreferredName
    Public Property formerNames As List(Of FormerName)
    Public Property birthName As BirthName
    Public Property birthPlace As BirthPlace
    Public Property otherPersonalAddresses As List(Of OtherPersonalAddress)
    Public Property raceCode As RaceCode
    Public Property citizenshipCountryCodes As List(Of CitizenshipCountryCode)
    Public Property residencyCountryCodes As List(Of ResidencyCountryCode)
    Public Property militaryStatusCode As MilitaryStatusCode
    Public Property militaryClassificationCodes As List(Of MilitaryClassificationCode)
    Public Property governmentIDs As List(Of GovernmentID)
    Public Property legalName As LegalName
    Public Property legalAddress As LegalAddress
    Public Property communication As Communication
End Class

Public Class WorkerDates
    Public Property originalHireDate As String
End Class

Public Class StatusCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class ReasonCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class WorkerStatus
    Public Property statusCode As StatusCode
    Public Property reasonCode As ReasonCode
End Class

Public Class NameCode11
    Public Property shortName As String
End Class

Public Class Link
    Public Property href As String
    Public Property rel As String
End Class

Public Class Photo
    Public Property itemID As String
    Public Property nameCode As NameCode11
    Public Property links As List(Of Link)
End Class

Public Class Landline2
    Public Property itemID As String
    Public Property countryDialing As String
    Public Property areaDialing As String
    Public Property dialNumber As String
    Public Property formattedNumber As String
End Class

Public Class Mobile2
    Public Property itemID As String
    Public Property countryDialing As String
    Public Property areaDialing As String
    Public Property dialNumber As String
    Public Property formattedNumber As String
End Class

Public Class Fax
    Public Property itemID As String
    Public Property countryDialing As String
    Public Property areaDialing As String
    Public Property dialNumber As String
    Public Property formattedNumber As String
End Class

Public Class Email2
    Public Property itemID As String
    Public Property emailUri As String
End Class

Public Class InternetAddress
    Public Property itemID As String
    Public Property uri As String
End Class

Public Class InstantMessage
    Public Property itemID As String
    Public Property uri As String
End Class

Public Class BusinessCommunication
    Public Property landlines As List(Of Landline2)
    Public Property mobiles As List(Of Mobile2)
    Public Property faxes As List(Of Fax)
    Public Property emails As List(Of Email2)
    Public Property internetAddresses As List(Of InternetAddress)
    Public Property instantMessages As List(Of InstantMessage)
End Class

Public Class StatusCode2
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class ReasonCode2
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class AssignmentStatus
    Public Property statusCode As StatusCode2
    Public Property reasonCode As ReasonCode2
End Class

Public Class WorkerTypeCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class AssignmentTermCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class WorkLevelCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class NationalityContextCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class HighlyCompensatedTypeCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class NameCode12
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class GroupCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class WorkerGroup
    Public Property nameCode As NameCode12
    Public Property groupCode As GroupCode
End Class

Public Class JobCode
    Public Property codeValue As String
End Class

Public Class NameCode13
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class ClassificationCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class OccupationalClassification
    Public Property itemID As String
    Public Property nameCode As NameCode13
    Public Property classificationCode As ClassificationCode
End Class

Public Class WageLawNameCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class WorkArrangementCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class UnitCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class StandardHours
    Public Property hoursQuantity As Integer
    Public Property unitCode As UnitCode
End Class

Public Class NameCode14
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class TypeCode5
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class HomeOrganizationalUnit
    Public Property itemID As String
    Public Property nameCode As NameCode14
    Public Property typeCode As TypeCode5
End Class

Public Class NameCode15
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class TypeCode6
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class AssignedOrganizationalUnit
    Public Property itemID As String
    Public Property nameCode As NameCode15
    Public Property typeCode As TypeCode6
End Class

Public Class CountrySubdivisionLevel17
    Public Property subdivisionType As String
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class Address4
    Public Property lineOne As String
    Public Property cityName As String
    Public Property countrySubdivisionLevel1 As CountrySubdivisionLevel17
    Public Property countryCode As String
    Public Property postalCode As String
End Class

Public Class NameCode16
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class HomeWorkLocation
    Public Property address As Address4
    Public Property nameCode As NameCode16
End Class

Public Class NameCode17
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class CountrySubdivisionLevel18
    Public Property subdivisionType As String
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class GeoCoordinate2
    Public Property latitude As Double
    Public Property longitude As Double
End Class

Public Class Address5
    Public Property lineOne As String
    Public Property cityName As String
    Public Property countrySubdivisionLevel1 As CountrySubdivisionLevel18
    Public Property countryCode As String
    Public Property postalCode As String
    Public Property geoCoordinate As GeoCoordinate2
End Class

Public Class AssignedWorkLocation
    Public Property nameCode As NameCode17
    Public Property address As Address5
End Class

Public Class RemunerationBasisCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class PayCycleCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class UnitCode2
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class StandardPayPeriodHours
    Public Property hoursQuantity As Integer
    Public Property unitCode As UnitCode2
End Class

Public Class MonthlyRateAmount
    Public Property amountValue As Double
    Public Property currencyCode As String
End Class

Public Class AnnualRateAmount
    Public Property amountValue As Integer
    Public Property currencyCode As String
End Class

Public Class PayPeriodRateAmount
    Public Property amountValue As Double
    Public Property currencyCode As String
End Class

Public Class BaseUnitCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class CommissionRatePercentage
    Public Property percentageValue As Double
    Public Property baseUnitCode As BaseUnitCode
End Class

Public Class BaseRemuneration
    Public Property monthlyRateAmount As MonthlyRateAmount
    Public Property annualRateAmount As AnnualRateAmount
    Public Property payPeriodRateAmount As PayPeriodRateAmount
    Public Property commissionRatePercentage As CommissionRatePercentage
End Class

Public Class TypeCode7
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class IntervalCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class NameCode18
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class UnitCode3
    Public Property codeValue As String
End Class

Public Class BaseUnitCode2
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class Rate
    Public Property amountValue As Integer
    Public Property unitCode As UnitCode3
    Public Property baseUnitCode As BaseUnitCode2
End Class

Public Class AdditionalRemuneration
    Public Property itemID As String
    Public Property typeCode As TypeCode7
    Public Property intervalCode As IntervalCode
    Public Property nameCode As NameCode18
    Public Property rate As Rate
End Class

Public Class SchemeCode2
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class WorkerID2
    Public Property idValue As String
    Public Property schemeCode As SchemeCode2
End Class

Public Class ReportsToWorkerName
    Public Property givenName As String
    Public Property familyName1 As String
    Public Property formattedName As String
End Class

Public Class ReportsToRelationshipCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class ReportsTo
    Public Property itemID As String
    Public Property positionID As String
    Public Property associateOID As String
    Public Property workerID As WorkerID2
    Public Property reportsToWorkerName As ReportsToWorkerName
    Public Property reportsToRelationshipCode As ReportsToRelationshipCode
End Class

Public Class PayrollProcessingStatusCode
    Public Property effectiveDate As String
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class PayScaleCode
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class PayGradeCode
    Public Property codeValue As String
End Class

Public Class BaseUnitCode3
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class MinimumRate
    Public Property amountValue As Integer
    Public Property currencyCode As String
    Public Property baseUnitCode As BaseUnitCode3
End Class

Public Class BaseUnitCode4
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class MedianRate
    Public Property amountValue As Integer
    Public Property currencyCode As String
    Public Property baseUnitCode As BaseUnitCode4
End Class

Public Class BaseUnitCode5
    Public Property codeValue As String
    Public Property shortName As String
End Class

Public Class MaximumRate
    Public Property amountValue As Integer
    Public Property currencyCode As String
    Public Property baseUnitCode As BaseUnitCode5
End Class

Public Class PayGradePayRange
    Public Property minimumRate As MinimumRate
    Public Property medianRate As MedianRate
    Public Property maximumRate As MaximumRate
End Class

Public Class Link2
    Public Property href As String
    Public Property rel As String
End Class

Public Class Worker
    Public Property associateOID As String
    Public Property workerID As WorkerID
    Public Property person As Person
    Public Property workerDates As WorkerDates
    Public Property workerStatus As WorkerStatus
    Public Property photos As List(Of Photo)
    Public Property businessCommunication As BusinessCommunication
    Public Property workAssignments As List(Of WorkAssignment)
    Public Property links As List(Of Link2)
End Class

Public Class RootObject
    Public Property workers As List(Of Worker)
End Class
End Class
json vb.net
1个回答
1
投票

您正在尝试将JSON直接反序列化为Workers.Person。它无法正确反序列化,因为您的JSON从Workers.Worker数组开始。您应该按如下方式修改反序列化

Dim workers = JsonConvert.DeserializeObject(Of Rootobject)(str)

然后,您可以浏览您的工作人员,因为它们将存储在Workers.Worker类型的数组中

Dim worker = workers[0]

并且可以按如下方式访问person属性

Dim person = worker.person
© www.soinside.com 2019 - 2024. All rights reserved.