Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

How to parse json and read in vb.net

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.

and this is my JSON code example:

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

My question:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could use JavaScriptSerializer which is in 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...