如何解析json并在vb.net中读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38943260/
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 18:33:27  来源:igfitidea点击:

How to parse json and read in vb.net

jsonvb.net

提问by Ali Emami

I have this code in my project:

我的项目中有这个代码:

Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader

request = DirectCast(WebRequest.Create("https://url.to.my.json"), HttpWebRequest)

response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())

Dim rawresp As String
rawresp = reader.ReadToEnd()
textbox2.text = rawresp

and TextBox2 gets the JSON code correctly.

和 TextBox2 正确获取 JSON 代码。

and this is my JSON code example:

这是我的 JSON 代码示例:

{
  "id":174543706,
  "first_name":"Hamed",
  "last_name":"Ap",
  "username":"hamed_ap",
  "type":"private"
}

My question:

我的问题:

How to get 174543706from JSON code ("id") into TextBox3.Text???

如何从 JSON 代码(“id”)获取174543706到 TextBox3.Text 中???

回答by Jim Hewitt

You could use JavaScriptSerializerwhich is in System.Web.Script.Serialization.

您可以使用JavaScriptSerializerwhich 在System.Web.Script.Serialization.

Imports System.Web.Script.Serialization

Module Module1
    Sub Main()

        Dim s As String

        Try
            Dim rawresp As String = "{""id"":174543706,""first_name"":""Hamed"",""last_name"":""Ap"",""username"":""hamed_ap"",""type"":""private""}"

            Dim jss As New JavaScriptSerializer()
            Dim dict As Dictionary(Of String, String) = jss.Deserialize(Of Dictionary(Of String, String))(rawresp)

            s = dict("id")
        Catch ex As Exception

        End Try

    End Sub

End Module

回答by MedAmine.Rihane

try this code :

试试这个代码:

Dim jsonResulttodict = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(rawresp)
Dim firstItem = jsonResulttodict.item ("id") 

hope it help you !!

希望对你有帮助!!

回答by Serafin William

How to get 174543706 from JSON code ("id") into TextBox3.Text?

如何从 JSON 代码(“id”)中获取 174543706 TextBox3.Text

{
  "id": 174543706,
  "first_name": "Hamed",
  "last_name": "Ap",
  "username": "hamed_ap",
  "type": "private"
}

Sorry if my reply was late. I hope my answer can help someone who's still confused. So what you do was get the response and read the JSON.

对不起,如果我的回复晚了。我希望我的回答可以帮助那些仍然困惑的人。所以你要做的是获取响应并读取 JSON。

After you do ReadToEnd():

完成后ReadToEnd()

Dim xr As XmlReader = XmlReader.Create(New StringReader(rawresp))
Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml(rawresp)

Then What you need to do is to read the data from the response. you do like this:

那么你需要做的是从响应中读取数据。你喜欢这样:

Dim res As String = JsonConvert.SerializeXmlNode(doc)
Dim ThisToken As JObject = Newtonsoft.Json.JsonConvert.DeserializeObject(Of JObject)(res)
Dim response As String = ThisToken("response").ToString()
Dim ThisData As JObject = Newtonsoft.Json.JsonConvert.DeserializeObject(Of JObject)(response)

After that yo can get the data from the response and convert it into string

之后你可以从响应中获取数据并将其转换为字符串

Dim idx As String = ThisData("id").ToString()

// the value of idx will be: 174543706

Then last you can put it into Texbox3.Text.

然后最后你可以把它放进去Texbox3.Text

回答by Ajay Rai

This works:

这有效:

Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader

request = DirectCast(WebRequest.Create("https://url.to.my.json"), HttpWebRequest)

response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())

Dim rawresp As String
rawresp = reader.ReadToEnd()


textbox2.text = JObject.Parse(rawresp)("id")