如何在 vb.net 中解析 json 数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29161949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:38:25  来源:igfitidea点击:

How to parse json array in vb.net?

.netarraysjsonvb.netparsing

提问by MAC

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

我正在学习JSON,我想解析 json 数组并使用VB.Net只获取一个值。我找到了这个问题的答案,但我似乎没有得到我想要的。根据提问者的说法,他有这个

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

and we can use this to parse json array

我们可以用它来解析 json 数组

   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 linksis 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 thisbecause 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.

PS这不是重复到这个,因为我不打算将信息添加到一个DataGridView。我在这里想要的是解析一个字段。只得到一个结果,而不是整个列表。

回答by Jameel Grand

You need to do use :

你需要使用:

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

go through with this link Deserialize Json

通过此链接反序列化 Json

回答by MAC

found this that can parse jsonproviding this libraryis installed.

发现这个可以解析json提供这个

        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 hereand thisis a code in c# form that i found... for reference only.

这里找到它,是我找到的 c# 形式的代码...仅供参考。