我的JSON字符串
{
"name": "username",
"place": {
"name": "placename",
}
我的代码到目前为止
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Try
request = DirectCast(WebRequest.Create("http://json-request.com/myjson"), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim rawresp As String
rawresp = reader.ReadToEnd()
Dim jResults As JObject = JObject.Parse(rawresp)
Dim results As List(Of JToken) = jResults.Children().ToList()
For Each item As JProperty In results
item.CreateReader()
textbox.text = ("placename"))
Next
Catch ex As Exception
MsgBox(ex.ToString)
Finally
If Not response Is Nothing Then response.Close()
End Try
End Sub
我如何从URL中提取并在一个文本框中解析地名,并在VB.net中解析另一个文本框中的用户名,如果它有助于我使用Visual Studio 2013和Windows窗体
非常感谢任何帮助
由于
答案 0 :(得分:0)
我假设rawresp
包含json,因为在此问题中已发布,以下代码片段演示了一种获取"用户名"和"地名",然后将它们显示在两个不同的TextBox
s:
......
Dim jResults As JObject = JsonConvert.DeserializeObject<JObject>(rawresp)
usernameTextbox.text = jResults("name").ToString()
placenameTextbox.text = jResults("place")("name").ToString()
......
答案 1 :(得分:0)
谢谢我必须将[]改为()并稍微改为
Dim jResults As JObject = JObject.Parse(rawresp)
usernameTextbox.text = jResults("name").ToString()
placenameTextbox.text = jResults("place")("name").ToString()
但它有效:D
但是当请求是404我得到一个异常抛出你有任何想法我如何处理错误或任何其他错误,因为我将使用文本框来更改URL并显示一个消息框?
再次感谢