简体   繁体   中英

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.

PS 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.

You need to do use :

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

go through with this link Deserialize Json

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM