MAC
MAC

Reputation: 686

How to parse json array in vb.net?

I am learning about JSON and I want to parse json array and get only one value using VB.Net. I found this QUESTION with answers but I didn't seem to get what I was looking for. According the the questioner, he has this

"links":[
  {
    "rel":"next",
    "href":"www.google.com"
  }
]

and we can use this to parse json array

   links(1).Item("rel")

or this

links(1)("rel")

What if I only have this?

[{
    "rel":"next",
    "href":"www.google.com"
}]

How should I code it without the word links? I understand that links is the table name, isn't it? I tried so many possibilities which give me more errors. I'd appreciate much if anyone can help me out.

P.S. This not a duplicate to this because I am not going to add the information to a DataGridView. What I want here is to parse a field. Get only one result and not the entire list.

Upvotes: 3

Views: 45127

Answers (2)

MAC
MAC

Reputation: 686

found this that can parse json providing this library is installed.

        Dim token As JToken
        Dim rel
        Dim href
        For Each value As Object In result
            token = JObject.Parse(value.ToString())
            rel = token.SelectToken("rel")
            href = token.SelectToken("href")

            Console.WriteLine("{0} {1}", rel, href)
        Next value

providing that this code is present

' Create a request for the URL. 
        Dim request As WebRequest = WebRequest.Create("http://")

        ' If required by the server, set the credentials.
        request.Credentials = CredentialCache.DefaultCredentials
        ' Get the response.
        Dim response As WebResponse = request.GetResponse()
        ' Display the status.
        Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
        ' Get the stream containing content returned by the server.
        Dim dataStream As Stream = response.GetResponseStream()
        ' Open the stream using a StreamReader for easy access.
        Dim reader As New StreamReader(dataStream)
        ' Read the content.
        Dim responseFromServer As String = reader.ReadToEnd()
        'Dim responseFromServer As String = reader.ReadToEnd()
        Console.WriteLine(responseFromServer)
        Dim result = JsonConvert.DeserializeObject(Of ArrayList)(responseFromServer)

and this code at the bottom

        Console.ReadKey()
        ' Clean up the streams and the response.
        reader.Close()
        response.Close()

with imports...

        Imports System
        Imports System.IO
        Imports System.Net
        Imports System.Text
        Imports Newtonsoft.Json
        Imports Newtonsoft.Json.Linq

found it here and this is a code in c# form that i found... for reference only.

Upvotes: 6

Jameel Mohammed
Jameel Mohammed

Reputation: 2364

You need to do use :

Imports Newtonsoft.Json
JsonConvert.DeserializeObject(Of <Your Class object>)(<JSON String>)

go through with this link Deserialize Json

Upvotes: 6

Related Questions