VB.Net_Having 从 JSON 读取令牌时出现问题
VB.Net_Having Problem with reading a token from JSON
我正在制作一个从 open weather map API
读取天气数据的程序
这是回复(json):
coord
lon -0.1257
lat 51.5085
weather
0
id 802
main "Clouds"
description "scattered clouds"
icon "03d"
base "stations"
main
temp 22.43
feels_like 22.4
temp_min 19.82
temp_max 24.21
pressure 1021
humidity 64
visibility 10000
wind
speed 4.12
deg 270
clouds
all 40
dt 1625834363
sys
type 2
id 2006068
country "GB"
sunrise 1625802859
sunset 1625861805
timezone 3600
id 2643743
name "London"
cod 200
这是我用来 select 来自 json 的代码:
Imports System.Net
Imports System.IO
Imports Newtonsoft.Json.Linq
Public deg As String = Chr(176)
Public iconid As String = ""
Public locationstr As String
Public ApiKey As String = "MYAPIKEY"
#Region "Conditions"
Public windspeedMPS As String = ""
Public humidity As String = ""
Public description As String
Public cloudiness As String = ""
Public temp As String = ""
Public City As String = ""
#End Region
Public Sub getweather(ByVal key As String)
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://api.openweathermap.org/data/2.5/weather?q=" & locationstr & "&appid=" & ApiKey & "&lang=en&units=metric"), HttpWebRequest)
Dim res As HttpWebResponse = DirectCast(req.GetResponse, HttpWebResponse)
Dim reader As New StreamReader(res.GetResponseStream)
Dim serverresponse As String = reader.ReadToEnd
Dim json As String = serverresponse
Dim obj As JObject = JObject.Parse(json)
Try
windspeddMPS = obj.SelectToken("wind").SelectToken("speed")
humidity = obj.SelectToken("main").SelectToken("humidity")
description = obj.SelectToken("weather").SelectToken("description")
cloudiness = obj.SelectToken("clouds").SelectToken("all")
temp = obj.SelectToken("main").SelectToken("temp")
City = obj.SelectToken("name")
iconid = obj.SelectToken("weather").SelectToken("icon")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
我可以 select 除了“描述”(组内的天气状况)和“图标”(天气状况图标 ID)令牌之外的所有令牌。
作为参考,这里是您正在使用的 API 网络文档:https://openweathermap.org/current
Visual Studio 有一个很酷的功能,称为将 JSON 粘贴为 类,可以在编辑 > 选择性粘贴 > 将 JSON 粘贴为 类 下找到。如果你这样做,那么你会得到如下所示的东西:
Public Class Rootobject
Public Property coord As Coord
Public Property weather() As Weather
Public Property base As String
Public Property main As Main
Public Property visibility As Integer
Public Property wind As Wind
Public Property clouds As Clouds
Public Property dt As Integer
Public Property sys As Sys
Public Property timezone As Integer
Public Property id As Integer
Public Property name As String
Public Property cod As Integer
End Class
Public Class Coord
Public Property lon As Single
Public Property lat As Single
End Class
Public Class Main
Public Property temp As Single
Public Property feels_like As Single
Public Property temp_min As Single
Public Property temp_max As Single
Public Property pressure As Integer
Public Property humidity As Integer
End Class
Public Class Wind
Public Property speed As Single
Public Property deg As Integer
End Class
Public Class Clouds
Public Property all As Integer
End Class
Public Class Sys
Public Property type As Integer
Public Property id As Integer
Public Property message As Single
Public Property country As String
Public Property sunrise As Integer
Public Property sunset As Integer
End Class
Public Class Weather
Public Property id As Integer
Public Property main As String
Public Property description As String
Public Property icon As String
End Class
从这里,您可以反序列化 JSON 对 Rootobject
class 的响应,这反过来您可以访问强类型对象以获取您的值:
' ... HTTP request to get JSON
Dim jsonObject = JsonConvert.DeserializeObject(Of Rootobject)(json)
Console.WriteLine(jsonObject.clouds.all)
我正在制作一个从 open weather map API
读取天气数据的程序这是回复(json):
coord
lon -0.1257
lat 51.5085
weather
0
id 802
main "Clouds"
description "scattered clouds"
icon "03d"
base "stations"
main
temp 22.43
feels_like 22.4
temp_min 19.82
temp_max 24.21
pressure 1021
humidity 64
visibility 10000
wind
speed 4.12
deg 270
clouds
all 40
dt 1625834363
sys
type 2
id 2006068
country "GB"
sunrise 1625802859
sunset 1625861805
timezone 3600
id 2643743
name "London"
cod 200
这是我用来 select 来自 json 的代码:
Imports System.Net
Imports System.IO
Imports Newtonsoft.Json.Linq
Public deg As String = Chr(176)
Public iconid As String = ""
Public locationstr As String
Public ApiKey As String = "MYAPIKEY"
#Region "Conditions"
Public windspeedMPS As String = ""
Public humidity As String = ""
Public description As String
Public cloudiness As String = ""
Public temp As String = ""
Public City As String = ""
#End Region
Public Sub getweather(ByVal key As String)
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://api.openweathermap.org/data/2.5/weather?q=" & locationstr & "&appid=" & ApiKey & "&lang=en&units=metric"), HttpWebRequest)
Dim res As HttpWebResponse = DirectCast(req.GetResponse, HttpWebResponse)
Dim reader As New StreamReader(res.GetResponseStream)
Dim serverresponse As String = reader.ReadToEnd
Dim json As String = serverresponse
Dim obj As JObject = JObject.Parse(json)
Try
windspeddMPS = obj.SelectToken("wind").SelectToken("speed")
humidity = obj.SelectToken("main").SelectToken("humidity")
description = obj.SelectToken("weather").SelectToken("description")
cloudiness = obj.SelectToken("clouds").SelectToken("all")
temp = obj.SelectToken("main").SelectToken("temp")
City = obj.SelectToken("name")
iconid = obj.SelectToken("weather").SelectToken("icon")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
我可以 select 除了“描述”(组内的天气状况)和“图标”(天气状况图标 ID)令牌之外的所有令牌。
作为参考,这里是您正在使用的 API 网络文档:https://openweathermap.org/current
Visual Studio 有一个很酷的功能,称为将 JSON 粘贴为 类,可以在编辑 > 选择性粘贴 > 将 JSON 粘贴为 类 下找到。如果你这样做,那么你会得到如下所示的东西:
Public Class Rootobject
Public Property coord As Coord
Public Property weather() As Weather
Public Property base As String
Public Property main As Main
Public Property visibility As Integer
Public Property wind As Wind
Public Property clouds As Clouds
Public Property dt As Integer
Public Property sys As Sys
Public Property timezone As Integer
Public Property id As Integer
Public Property name As String
Public Property cod As Integer
End Class
Public Class Coord
Public Property lon As Single
Public Property lat As Single
End Class
Public Class Main
Public Property temp As Single
Public Property feels_like As Single
Public Property temp_min As Single
Public Property temp_max As Single
Public Property pressure As Integer
Public Property humidity As Integer
End Class
Public Class Wind
Public Property speed As Single
Public Property deg As Integer
End Class
Public Class Clouds
Public Property all As Integer
End Class
Public Class Sys
Public Property type As Integer
Public Property id As Integer
Public Property message As Single
Public Property country As String
Public Property sunrise As Integer
Public Property sunset As Integer
End Class
Public Class Weather
Public Property id As Integer
Public Property main As String
Public Property description As String
Public Property icon As String
End Class
从这里,您可以反序列化 JSON 对 Rootobject
class 的响应,这反过来您可以访问强类型对象以获取您的值:
' ... HTTP request to get JSON
Dim jsonObject = JsonConvert.DeserializeObject(Of Rootobject)(json)
Console.WriteLine(jsonObject.clouds.all)