0

I try to get the values from the basic section in the following json result

   {
  "responseCode": "Ok",
  "responseMessage": "",
  "ssnStatus": "Active",
  "basic": {
    "firstName": "Testnamn",
    "givenName": "Gettnamn",
    "surName": "Testname",
    "middleName": null,
    "lastName": "Lastname",
    "co": null,
    "street": "Teststreet lgh 1202",
    "zipCode": "92609",
    "city": "Stad"
  },
  "phoneNumbers": {
    "phoneNumbers": []
  },
  "ssnStatusBlock": null
}

I can get the first level (ssnStatus) with the code below, but how do I get the firstName, givenName etc?

Dim post As Post = JsonConvert.DeserializeObject(Of Post)(exampleJson)
                    Dim ssnStatus As String = post.ssnStatus

and

Public Class Post
        Public Property ssnStatus As String
End Class

1 Answer 1

2

You are missing the properties and classes definition for all the other membersof the JSON object.

  • Create a new class file in your Project. Give it a name that properly describe the JSON usage,
  • add the Imports Newtonsoft.Json import,
  • copy a sample of the JSON object that describes the JSON structure (the one you have here is good),
  • position the caret inside the new class definition,
  • see the Visual Studio menu: Edit -> Paste Special -> Paste JSON as classes

Visual Studio will create all the classes and properties needed to handle the JSON object you selected.

Note: With more complex classes, it may happen that the result Visual Studio produces is not exactly what you require. In this case, try one of the specialized WebSites that provide a free conversion service:

The new root class definition will be named Rootobject. Change this name as needed,
to make it more clear what the class is used for.

This is the class definition that Visual Studio creates with the JSON object in your question.
I created a class Project class named MyWebSitePost, created the JSON bject class definition as previously described, I then renamed the default master class Post, replacing the default Rootobject name:

Public Class MyWebSitePost

    Public Class Post
        Public Property responseCode As String
        Public Property responseMessage As String
        Public Property ssnStatus As String
        Public Property basic As Basic
        Public Property phoneNumbers As Phonenumbers
        Public Property ssnStatusBlock As Object
    End Class

    Public Class Basic
        Public Property firstName As String
        Public Property givenName As String
        Public Property surName As String
        Public Property middleName As Object
        Public Property lastName As String
        Public Property co As Object
        Public Property street As String
        Public Property zipCode As String
        Public Property city As String
    End Class

    Public Class Phonenumbers
        Public Property phoneNumbers() As Object
    End Class
End Class

You can then use the code you already have to access all the other properties:

(Some Properties type may have been set to Object; modify as required).

Dim JsonPost As Post = JsonConvert.DeserializeObject(Of Post)(exampleJson)
Dim ssnStatus As String = JsonPost.ssnStatus
Dim FirstName As String = JsonPost.basic.firstName

and so on.

Note:
As you can see, all properties have the default name, as defined in the JSON object. Some properties names are not really adeguate to describe thier content. For example the co property is probably the Country name. To change the property name, you can use the <JsonProperty> attribute, which references the JSON object original name and use a custom name for the Property:

'(...)
<JsonProperty("co")>
Public Property Country As Object
'(...)

You can then access this Property using the custom name:

Dim CountryName As String = JsonPost.basic.Country
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.