我的项目中有这个代码:
for($x = 0;$x < count($lessons['class_name']); $x++)
和TextBox2正确获取JSON代码。
这是我的JSON代码示例:
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
我的问题:
如何从JSON代码(&#34; id&#34;)获取 174543706 到TextBox3.Text ???
答案 0 :(得分:9)
您可以使用JavaScriptSerializer
中的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
答案 1 :(得分:4)
试试这段代码:
Dim jsonResulttodict = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(rawresp)
Dim firstItem = jsonResulttodict.item ("id")
希望它可以帮到你!!
答案 2 :(得分:1)
如何从JSON代码(“ id”)中获取174543706到TextBox3.Text
中?
{
"id": 174543706,
"first_name": "Hamed",
"last_name": "Ap",
"username": "hamed_ap",
"type": "private"
}
对不起,如果我的回复迟到了。我希望我的回答可以帮助仍然困惑的人。 因此,您要做的就是获取响应并读取JSON。
完成ReadToEnd()
后:
Dim xr As XmlReader = XmlReader.Create(New StringReader(rawresp))
Dim doc As XmlDocument = New XmlDocument()
doc.LoadXml(rawresp)
然后,您需要做的是从响应中读取数据。你确实这样:
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)
之后,您可以从响应中获取数据并将其转换为字符串
Dim idx As String = ThisData("id").ToString()
// the value of idx will be: 174543706
最后,您可以将其放入Texbox3.Text
。
答案 3 :(得分:0)
这有效:
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")