'Can't pass the value to the class using deserialization (JSON) - VB.net
Good day!
How to pass the value to the class using deserialization (JSON).
In the code the value of my Class is "Nothing"
This the value of jsonSerialize
{
"getBPDetailsResponse":{
"pageSize":"22",
"pageNum":"1",
"noOfPages":"1",
"totalNoOfRecords":"8",
"hasNext":"N",
"documentDetails":{
"documentDetails":[
{
"customerId":"100819",
"documentLevel":"41",
"documentPurpose":"2",
"documentSubmittedId":null,
"documentStatus":null,
"documentCode":"45",
"documentTransactionNumber":"100000",
"issueDate":null,
"receivedDate":null,
"remarks":null
},
{
"customerId":"100819",
"documentLevel":"41",
"documentPurpose":"2",
"documentSubmittedId":null,
"documentStatus":null,
"documentCode":"65",
"documentTransactionNumber":"100008",
"issueDate":null,
"receivedDate":null,
"remarks":null
},
{
"customerId":"100819",
"documentLevel":"41",
"documentPurpose":"2",
"documentSubmittedId":null,
"documentStatus":null,
"documentCode":"325",
"documentTransactionNumber":"100009",
"issueDate":null,
"receivedDate":null,
"remarks":null
},
{
"customerId":"100819",
"documentLevel":"41",
"documentPurpose":"59",
"documentSubmittedId":null,
"documentStatus":null,
"documentCode":"460",
"documentTransactionNumber":"100010",
"issueDate":null,
"receivedDate":null,
"remarks":null
},
{
"customerId":"100819",
"documentLevel":"41",
"documentPurpose":"58",
"documentSubmittedId":null,
"documentStatus":null,
"documentCode":"459",
"documentTransactionNumber":"100011",
"issueDate":null,
"receivedDate":null,
"remarks":null
},
{
"customerId":"100819",
"documentLevel":"41",
"documentPurpose":"2",
"documentSubmittedId":null,
"documentStatus":null,
"documentCode":"104",
"documentTransactionNumber":"100012",
"issueDate":null,
"receivedDate":null,
"remarks":null
},
{
"customerId":"100819",
"documentLevel":"41",
"documentPurpose":"35",
"documentSubmittedId":null,
"documentStatus":null,
"documentCode":"151",
"documentTransactionNumber":"100013",
"issueDate":null,
"receivedDate":null,
"remarks":null
},
{
"customerId":"100819",
"documentLevel":"41",
"documentPurpose":"2",
"documentSubmittedId":null,
"documentStatus":null,
"documentCode":"92",
"documentTransactionNumber":"100014",
"issueDate":null,
"receivedDate":null,
"remarks":null
}
]
},
"customerDetails":{
"relationshipmgr":"100004",
"blackListedStatus":"2",
"customerCategory":"1",
"customerId":"100819",
"externalReference":null,
"status":"4",
"countryOfRisk":"PH",
"customerOpendate":"20211122",
"domicile":"PH",
"dateOfOrigin":"19781121",
"familyName":"REYES NEW ",
"familyNameLocalLang":"Reyes New ",
"firstNameLocalLang":"Abigail",
"middleName":"AYRAN",
"nationality":"PH",
"entityCode":"GCTBCPH001",
"customerName":"ABIGAIL AYRAN REYES NEW ",
"customerNameLocalLng":"Abigail Reyes New ",
"customerRole":"1",
"branchCode":"015",
"customerSegment":"17"
},
"individualDetails":{
"birthPlace":"PH",
"firstName":"ABIGAIL",
"middleNameLocalLang":null,
"maritalStatus":"2",
"motherName":"Mandatory Mother",
"gender":"2",
"salutation":null,
"fatherName":null
},
"corporateDetails":{
"primarySector":null,
"secondarySector":null,
"legalForm":null,
"countryOfIncorp":null,
"countryOfActivity":null,
"shareHolderDeclaration":null
},
"taxDetails":{
"customerId":"100819",
"paysTaxesInUs":"2",
"taxCategory":"24",
"entityCode":"GCTBCPH001",
"exemptionCode":"Z",
"taxCountry":"PH",
"taxType":"4"
},
"addressDetails":{
"addressType":"Permanent",
"addressPurpose":"2",
"postalCode":"1234",
"countryCode":"AF",
"addressLine3":null,
"city":"12314",
"addressLine2":"124",
"addressLine4":null,
"state":"1234",
"validFrom":"20211122",
"entityCode":null,
"addressLine1":"1234",
"corrrespondenceFlag":"1",
"addressline3inLocalLanguage":null,
"addressline4inLocalLanguage":null,
"addressLine5":null,
"countryinLocalLanguage":null,
"cityinLocalLanguage":null,
"stateinLocalLanguage":null
},
"contactDetails":{
"addressPurpose":"35",
"customerId":"100819",
"secondaryEmailId":null,
"firstTelephoneNumber":null,
"nationalNumber":"1234567890",
"primaryContactIndicator":null,
"secondTelephoneNumber":null,
"website":null,
"entityCode":null,
"primaryEmailId":null
},
"identificationDetails":{
"identificationDetails":{
"issueDate":null,
"validTill":"20211204",
"issueAuthority":null,
"idIssuePlace":null,
"entityCode":"GCTBCPH001",
"identifierType":"621",
"identifierValue":"5656756",
"customerId":"100819",
"issueCountry":"PH"
}
},
"consentInformation":null
}
}
This is the code
response = request.GetResponse()
Console.WriteLine((CType(response, HttpWebResponse)).StatusDescription)
dataStream = response.GetResponseStream()
reader = New StreamReader(dataStream)
responseFromServer = reader.ReadToEnd()
'txtResponse.Text = responseFromServer
reader.Close()
response.Close()
Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml(responseFromServer)
Dim jsonSerialize = JsonConvert.SerializeXmlNode(doc)
Dim result As Customer = JsonConvert.DeserializeObject(Of Customer)(jsonSerialize)
This the Class
Public Class Customer
Public Property Results As Results
End Class
Public Class Results
Public Property pageSize As Long
Public Property pageNum As Long
Public Property noOfPages As Long
Public Property documentDetails As List(Of DocumentDetail)
Public Property totalNoOfRecords As Long
Public Property hasNext As String
Public Property customerDetails As CustomerDetails
Public Property individualDetails As IndividualDetails
Public Property corporateDetails As CorporateDetails
Public Property taxDetails As TaxDetails
Public Property addressDetails As AddressDetails
Public Property contactDetails As ContactDetails
Public Property identificationDetails As List(Of IdentificationDetail)
Public Property consentInformation As List(Of ConsentInformation)
End Class
etcccc
May I know what is the cause of this or possible Solution.
Solution 1:[1]
Here is a set of classes from jsonutils.com that should be suitable for deser'ing the given JSON, using Newtonsoft.
They needed some tweaking because the json is a bit awkward documentDetails is an object that has a property called documentDetails which is an array of objects) and as a result JU had made two classes called DocumentDetails.
As one is essentially just a holder for a DocumentDetail array, I called the holder class DocumentDetailCollection. Also, I renamed all the classes away from being plural; plural named classes aren't a great idea. There is still some room for improvement, as a lot of null properties have been put as Object. A more complete JSON would help, but you can alter these depending on what you know about the data:
Public Class DocumentDetail
<JsonProperty("customerId")>
Public Property CustomerId As String
<JsonProperty("documentLevel")>
Public Property DocumentLevel As String
<JsonProperty("documentPurpose")>
Public Property DocumentPurpose As String
<JsonProperty("documentSubmittedId")>
Public Property DocumentSubmittedId As Object
<JsonProperty("documentStatus")>
Public Property DocumentStatus As Object
<JsonProperty("documentCode")>
Public Property DocumentCode As String
<JsonProperty("documentTransactionNumber")>
Public Property DocumentTransactionNumber As String
<JsonProperty("issueDate")>
Public Property IssueDate As Object
<JsonProperty("receivedDate")>
Public Property ReceivedDate As Object
<JsonProperty("remarks")>
Public Property Remarks As Object
End Class
Public Class DocumentDetailCollection
<JsonProperty("documentDetails")>
Public Property DocumentDetails As DocumentDetail()
End Class
Public Class CustomerDetail
<JsonProperty("relationshipmgr")>
Public Property Relationshipmgr As String
<JsonProperty("blackListedStatus")>
Public Property BlackListedStatus As String
<JsonProperty("customerCategory")>
Public Property CustomerCategory As String
<JsonProperty("customerId")>
Public Property CustomerId As String
<JsonProperty("externalReference")>
Public Property ExternalReference As Object
<JsonProperty("status")>
Public Property Status As String
<JsonProperty("countryOfRisk")>
Public Property CountryOfRisk As String
<JsonProperty("customerOpendate")>
Public Property CustomerOpendate As String
<JsonProperty("domicile")>
Public Property Domicile As String
<JsonProperty("dateOfOrigin")>
Public Property DateOfOrigin As String
<JsonProperty("familyName")>
Public Property FamilyName As String
<JsonProperty("familyNameLocalLang")>
Public Property FamilyNameLocalLang As String
<JsonProperty("firstNameLocalLang")>
Public Property FirstNameLocalLang As String
<JsonProperty("middleName")>
Public Property MiddleName As String
<JsonProperty("nationality")>
Public Property Nationality As String
<JsonProperty("entityCode")>
Public Property EntityCode As String
<JsonProperty("customerName")>
Public Property CustomerName As String
<JsonProperty("customerNameLocalLng")>
Public Property CustomerNameLocalLng As String
<JsonProperty("customerRole")>
Public Property CustomerRole As String
<JsonProperty("branchCode")>
Public Property BranchCode As String
<JsonProperty("customerSegment")>
Public Property CustomerSegment As String
End Class
Public Class IndividualDetail
<JsonProperty("birthPlace")>
Public Property BirthPlace As String
<JsonProperty("firstName")>
Public Property FirstName As String
<JsonProperty("middleNameLocalLang")>
Public Property MiddleNameLocalLang As Object
<JsonProperty("maritalStatus")>
Public Property MaritalStatus As String
<JsonProperty("motherName")>
Public Property MotherName As String
<JsonProperty("gender")>
Public Property Gender As String
<JsonProperty("salutation")>
Public Property Salutation As Object
<JsonProperty("fatherName")>
Public Property FatherName As Object
End Class
Public Class CorporateDetail
<JsonProperty("primarySector")>
Public Property PrimarySector As Object
<JsonProperty("secondarySector")>
Public Property SecondarySector As Object
<JsonProperty("legalForm")>
Public Property LegalForm As Object
<JsonProperty("countryOfIncorp")>
Public Property CountryOfIncorp As Object
<JsonProperty("countryOfActivity")>
Public Property CountryOfActivity As Object
<JsonProperty("shareHolderDeclaration")>
Public Property ShareHolderDeclaration As Object
End Class
Public Class TaxDetail
<JsonProperty("customerId")>
Public Property CustomerId As String
<JsonProperty("paysTaxesInUs")>
Public Property PaysTaxesInUs As String
<JsonProperty("taxCategory")>
Public Property TaxCategory As String
<JsonProperty("entityCode")>
Public Property EntityCode As String
<JsonProperty("exemptionCode")>
Public Property ExemptionCode As String
<JsonProperty("taxCountry")>
Public Property TaxCountry As String
<JsonProperty("taxType")>
Public Property TaxType As String
End Class
Public Class AddressDetail
<JsonProperty("addressType")>
Public Property AddressType As String
<JsonProperty("addressPurpose")>
Public Property AddressPurpose As String
<JsonProperty("postalCode")>
Public Property PostalCode As String
<JsonProperty("countryCode")>
Public Property CountryCode As String
<JsonProperty("addressLine3")>
Public Property AddressLine3 As Object
<JsonProperty("city")>
Public Property City As String
<JsonProperty("addressLine2")>
Public Property AddressLine2 As String
<JsonProperty("addressLine4")>
Public Property AddressLine4 As Object
<JsonProperty("state")>
Public Property State As String
<JsonProperty("validFrom")>
Public Property ValidFrom As String
<JsonProperty("entityCode")>
Public Property EntityCode As Object
<JsonProperty("addressLine1")>
Public Property AddressLine1 As String
<JsonProperty("corrrespondenceFlag")>
Public Property CorrrespondenceFlag As String
<JsonProperty("addressline3inLocalLanguage")>
Public Property Addressline3inLocalLanguage As Object
<JsonProperty("addressline4inLocalLanguage")>
Public Property Addressline4inLocalLanguage As Object
<JsonProperty("addressLine5")>
Public Property AddressLine5 As Object
<JsonProperty("countryinLocalLanguage")>
Public Property CountryinLocalLanguage As Object
<JsonProperty("cityinLocalLanguage")>
Public Property CityinLocalLanguage As Object
<JsonProperty("stateinLocalLanguage")>
Public Property StateinLocalLanguage As Object
End Class
Public Class ContactDetail
<JsonProperty("addressPurpose")>
Public Property AddressPurpose As String
<JsonProperty("customerId")>
Public Property CustomerId As String
<JsonProperty("secondaryEmailId")>
Public Property SecondaryEmailId As Object
<JsonProperty("firstTelephoneNumber")>
Public Property FirstTelephoneNumber As Object
<JsonProperty("nationalNumber")>
Public Property NationalNumber As String
<JsonProperty("primaryContactIndicator")>
Public Property PrimaryContactIndicator As Object
<JsonProperty("secondTelephoneNumber")>
Public Property SecondTelephoneNumber As Object
<JsonProperty("website")>
Public Property Website As Object
<JsonProperty("entityCode")>
Public Property EntityCode As Object
<JsonProperty("primaryEmailId")>
Public Property PrimaryEmailId As Object
End Class
Public Class IdentificationDetail
<JsonProperty("issueDate")>
Public Property IssueDate As Object
<JsonProperty("validTill")>
Public Property ValidTill As String
<JsonProperty("issueAuthority")>
Public Property IssueAuthority As Object
<JsonProperty("idIssuePlace")>
Public Property IdIssuePlace As Object
<JsonProperty("entityCode")>
Public Property EntityCode As String
<JsonProperty("identifierType")>
Public Property IdentifierType As String
<JsonProperty("identifierValue")>
Public Property IdentifierValue As String
<JsonProperty("customerId")>
Public Property CustomerId As String
<JsonProperty("issueCountry")>
Public Property IssueCountry As String
End Class
Public Class IdentificationDetailCollection
<JsonProperty("identificationDetails")>
Public Property IdentificationDetails As IdentificationDetailCollection
End Class
Public Class GetBPDetailsResponse
<JsonProperty("pageSize")>
Public Property PageSize As String
<JsonProperty("pageNum")>
Public Property PageNum As String
<JsonProperty("noOfPages")>
Public Property NoOfPages As String
<JsonProperty("totalNoOfRecords")>
Public Property TotalNoOfRecords As String
<JsonProperty("hasNext")>
Public Property HasNext As String
<JsonProperty("documentDetails")>
Public Property DocumentDetails As DocumentDetailCollection
<JsonProperty("customerDetails")>
Public Property CustomerDetail As CustomerDetail
<JsonProperty("individualDetails")>
Public Property IndividualDetail As IndividualDetail
<JsonProperty("corporateDetails")>
Public Property CorporateDetail As CorporateDetail
<JsonProperty("taxDetails")>
Public Property TaxDetail As TaxDetail
<JsonProperty("addressDetails")>
Public Property AddressDetail As AddressDetail
<JsonProperty("contactDetails")>
Public Property ContactDetail As ContactDetail
<JsonProperty("identificationDetails")>
Public Property IdentificationDetail As IdentificationDetail
<JsonProperty("consentInformation")>
Public Property ConsentInformation As Object
End Class
Public Class Root
<JsonProperty("getBPDetailsResponse")>
Public Property GetBPDetailsResponse As GetBPDetailsResponse
End Class
Example usage, to get the customerId of the first documentdetails:
result.GetBPDetailsResponse.DocumentDetails.DocumentDetails.First().CustomerId
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Caius Jard |

