Home > Software design >  How to convert Json String to Json array in Visual Basic
How to convert Json String to Json array in Visual Basic

Time:10-07

I am working UWP (Visual Basic) I am fetching data from one site which replies in JSON string format.

[
    {"id": 1, "name": "Johny", "lname": "Sins", "phone": 123456789}, 
    {"id": 2, "name": "Nike", "lname": "Jons", "phone": 23456789}
]

here is my code how I get it: -

Dim url As String = "http://127.0.0.1:8000/getdata/"
     
Dim Request As HttpWebRequest = HttpWebRequest.Create(url)
Request.Proxy = Nothing
Request.UserAgent = "Test"

Dim Response As HttpWebResponse = Request.GetResponse
Dim ResponseStream As System.IO.Stream = Response.GetResponseStream

Dim StreamReader As New System.IO.StreamReader(ResponseStream)
Dim Data As String = StreamReader.ReadToEnd
StreamReader.Close()

Now I want to print this data One by One in one text box, so how do I convert in to json array and print it?

CodePudding user response:

I tested the parsing part with your Json data. Please refer to the method

Public Class node
      Public Property id As Integer
      Public Property name As String
      Public Property lname As String
      Public Property phone As Integer
End Class

   Public Sub JsonTest()

      Try

         Dim json_test As String = "[
                                  {'id': 1, 'name': 'Johny', 'lname': 'Sins', 'phone': 123456789}, 
                                  {'id': 2, 'name': 'Nike', 'lname': 'Jons', 'phone': 23456789}
                                ] "

         Dim nodelist As List(Of node) = JsonConvert.DeserializeObject(Of List(Of node))(json_test)

         For Each n In nodelist

            Console.WriteLine(n.id)
            Console.WriteLine(n.name)
            Console.WriteLine(n.lname)

         Next

      Catch ex As Exception
         MsgBox(ex.ToString)
      End Try

   End Sub
  • Related