0

Thanks in advance I have a json array like below [ { "Name": "abc", "Age": "15", "Gender": "M" }, { "Name": "def", "Age": "16", "Gender": "M" }, { "Name": "ghi", "Age": "17", "Gender": "M" } ]

i need to extract data, please advice

1
  • You can deserialize to a List(Of class) objects. Is this all the JSON you have, or there's more of it? If that's the case, post the rest of it. To build class objects from the JSON structure, you can use the corresponding Visual Studio's Paste Special tool, if the structure is simple. You can find on-line tools that do a better job, if the structure is more complex. What tools are you planning to use? Json.Net? JavascriptSerialier? System.Text.Json? Other? Commented Jun 6, 2020 at 10:44

1 Answer 1

1

Something like this should help get you started.
First we create the Person object like so

Public Class Person
Public Property Name As String
Public Property Age As String
Public Property Gender As String

Public Overrides Function ToString() As String
    Return "Name: " + Name + " Age: " + Age + " Gender: " + Gender
End Function
End Class

This will be were we store the persons properties.
We will also override the ToString() so we can get all of the properties we want when displaying the objects information.

Now we will Deserialize the payload into a Person Array using Newtonsoft Json, this is a very popular library to help you with any work needed for Json Data, you will need to install the package via Nuget

Dim payload = "[{""Name"": ""abc"",""Age"": ""15"",""Gender"": ""M""},{""Name"": ""def"",""Age"": ""16"",""Gender"": ""M""},{""Name"": ""ghi"",""Age"": ""17"", ""Gender"": ""M""}]"
Dim personCollection() = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Person())(payload)

Once that is complete, we can loop through each of the items in personCollection and display the results to the console.

For Each person As Person In personCollection
    Console.WriteLine(person.ToString())
Next

Hopefully this helps. I used a Console Application for this example.

Full Example

Module Module1

Sub Main()
    Dim payload = "[{""Name"": ""abc"",""Age"": ""15"",""Gender"": ""M""},{""Name"": ""def"",""Age"": ""16"",""Gender"": ""M""},{""Name"": ""ghi"",""Age"": ""17"", ""Gender"": ""M""}]"
    Dim personCollection() = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Person())(payload)

    For Each person As Person In personCollection
        Console.WriteLine(person.ToString())
    Next

    Console.ReadKey()

End Sub

End Module

Public Class Person
Public Property Name As String
Public Property Age As String
Public Property Gender As String

Public Overrides Function ToString() As String
    Return "Name: " + Name + " Age: " + Age + " Gender: " + Gender
End Function
End Class
Sign up to request clarification or add additional context in comments.

2 Comments

This worked as i expected , Thanks.I need to capture this json in POST Web Api, i passed from fiddler but getting nothing, please advice
@RoadsideRomeozz Your question has be answered. Please accept AndrewE's answer by clicking the check mark (tick mark) to the left of the answer. What you ask in your comment is a new question and should be posted as such.

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.