我有一个API调用(来自Domoticz家庭控制软件)返回的JSON结果,我试图使用Newtonsoft在VB.NET Web应用程序中对其进行解析。看起来很像:
{
"ActTime" : 1555786428,
"AstrTwilightEnd" : "21:22",
"AstrTwilightStart" : "05:38",
"CivTwilightEnd" : "20:23",
"CivTwilightStart" : "06:36",
"DayLength" : "12:59",
"NautTwilightEnd" : "20:52",
"NautTwilightStart" : "06:07",
"ServerTime" : "2019-04-20 14:53:48",
"SunAtSouth" : "13:05",
"Sunrise" : "07:00",
"Sunset" : "19:59",
"app_version" : "4.9700",
"result" : [
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"CustomImage" : 0,
"Data" : "75.7 F",
"Description" : "",
"Favorite" : 1,
"HardwareID" : 2,
"HardwareName" : "mygateway1",
"HardwareType" : "MySensors Gateway with MQTT interface",
"HardwareTypeVal" : 92,
"HaveTimeout" : false,
"ID" : "6600",
"LastUpdate" : "2019-04-20 14:53:47",
"Name" : "Dining Room",
"Notifications" : "false",
"PlanID" : "0",
"PlanIDs" : [ 0 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "LaCrosse TX3",
"Temp" : 75.700000000000003,
"Timers" : "false",
"Type" : "Temp",
"TypeImg" : "temperature",
"Unit" : 0,
"Used" : 1,
"XOffset" : "0",
"YOffset" : "0",
"idx" : "3"
},
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"CustomImage" : 0,
"Data" : "72.5 F",
"Description" : "",
"Favorite" : 1,
"HardwareID" : 2,
"HardwareName" : "mygateway1",
"HardwareType" : "MySensors Gateway with MQTT interface",
"HardwareTypeVal" : 92,
"HaveTimeout" : true,
"ID" : "6500",
"LastUpdate" : "2018-12-01 14:51:37",
"Name" : "Living Room",
"Notifications" : "false",
"PlanID" : "0",
"PlanIDs" : [ 0 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "LaCrosse TX3",
"Temp" : 72.5,
"Timers" : "false",
"Type" : "Temp",
"TypeImg" : "temperature",
"Unit" : 0,
"Used" : 1,
"XOffset" : "0",
"YOffset" : "0",
"idx" : "2"
},
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"BatteryLevel" : 255,
"Chill" : 69.799999999999997,
"CustomImage" : 0,
"Data" : "341.00;NNW;66;78;21.0;21.0",
"Description" : "",
"Direction" : 341.0,
"DirectionStr" : "NNW",
"Favorite" : 1,
"Gust" : "17.4",
"HardwareID" : 3,
"HardwareName" : "DarkSky",
"HardwareType" : "DarkSky (Weather Lookup)",
"HardwareTypeVal" : 25,
"HaveTimeout" : false,
"ID" : "0001",
"LastUpdate" : "2019-04-20 14:50:51",
"Name" : "DSwind",
"Notifications" : "false",
"PlanID" : "0",
"PlanIDs" : [ 0 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"Speed" : "14.8",
"SubType" : "TFA",
"Temp" : 69.799999999999997,
"Timers" : "false",
"Type" : "Wind",
"TypeImg" : "wind",
"Unit" : 0,
"Used" : 1,
"XOffset" : "0",
"YOffset" : "0",
"forecast_url" : "aHR0cHM6Ly9kYXJrc2t5Lm5ldC8jL2YvMjguMzM1NTY3JTJDLTgyLjY3MTY2MQ==",
"idx" : "5"
},
{
"AddjMulti" : 1.0,
"AddjMulti2" : 1.0,
"AddjValue" : 0.0,
"AddjValue2" : 0.0,
"Barometer" : 1016,
"BatteryLevel" : 255,
"CustomImage" : 0,
"Data" : "69.8 F, 43 %, 1016 hPa",
"Description" : "",
"DewPoint" : "46.30",
"Favorite" : 0,
"Forecast" : 1,
"ForecastStr" : "Sunny",
"HardwareID" : 3,
"HardwareName" : "DarkSky",
"HardwareType" : "DarkSky (Weather Lookup)",
"HardwareTypeVal" : 25,
"HaveTimeout" : false,
"Humidity" : 43,
"HumidityStatus" : "Comfortable",
"ID" : "0001",
"LastUpdate" : "2019-04-20 14:50:51",
"Name" : "DStemp",
"Notifications" : "false",
"PlanID" : "0",
"PlanIDs" : [ 0 ],
"Protected" : false,
"ShowNotifications" : true,
"SignalLevel" : "-",
"SubType" : "THB1 - BTHR918, BTHGN129",
"Temp" : 69.799999999999997,
"Timers" : "false",
"Type" : "Temp + Humidity + Baro",
"TypeImg" : "temperature",
"Unit" : 1,
"Used" : 1,
"XOffset" : "0",
"YOffset" : "0",
"forecast_url" : "aHR0cHM6Ly9kYXJrc2t5Lm5ldC8jL2YvMjguMzM1NTY3JTJDLTgyLjY3MTY2MQ==",
"idx" : "4"
}
],
"status" : "OK",
"title" : "Devices"
}
我有两节课:
Public Class DomoticzDevices_Rootobject
Public Property ActTime As Integer
Public Property AstrTwilightEnd As String
Public Property AstrTwilightStart As String
Public Property CivTwilightEnd As String
Public Property CivTwilightStart As String
Public Property DayLength As String
Public Property NautTwilightEnd As String
Public Property NautTwilightStart As String
Public Property ServerTime As String
Public Property SunAtSouth As String
Public Property Sunrise As String
Public Property Sunset As String
Public Property app_version As String
Public Property result As List(Of DomoticzDevices_result)
Public Property status As String
Public Property title As String
End Class
Public Class DomoticzDevices_result
Public Property Data As String
Public Property Description As String
Public Property Favorite As Integer
Public Property LastUpdate As String
Public Property Name As String
Public Property PlanID As String
Public Property PlanIDs() As Integer
Public Property _Protected As Boolean
Public Property Status As String
Public Property Type As String
Public Property TypeImg As String
Public Property UsedByCamera As Boolean
Public Property XOffset As Object
Public Property YOffset As Object
Public Property idx As String
Public Property AddjMulti As Single
Public Property AddjMulti2 As Single
Public Property AddjValue As Single
Public Property AddjValue2 As Single
Public Property BatteryLevel As Integer
Public Property CustomImage As Integer
Public Property HardwareID As Integer
Public Property HardwareName As String
Public Property HardwareType As String
Public Property HardwareTypeVal As Integer
Public Property HaveTimeout As Boolean
Public Property ID As String
Public Property Notifications As String
Public Property ShowNotifications As Boolean
Public Property SignalLevel As String
Public Property SubType As String
Public Property Temp As Single
Public Property Timers As String
Public Property Unit As Integer
Public Property Used As Integer
Public Property UVI As String
Public Property forecast_url As String
Public Property Rain As String
Public Property RainRate As String
Public Property Chill As Single
Public Property Direction As Single
Public Property DirectionStr As String
Public Property Gust As String
Public Property Speed As String
Public Property Barometer As Integer
Public Property DewPoint As String
Public Property Forecast As Integer
Public Property ForecastStr As String
Public Property Humidity As Integer
Public Property HumidityStatus As String
Public Property SwitchTypeVal As Integer
Public Property Visibility As Single
Public Property Image As String
Public Property SensorType As Integer
Public Property SensorUnit As String
Public Property CameraIdx As String
Public Property DimmerType As String
Public Property HaveDimmer As Boolean
Public Property HaveGroupCmd As Boolean
Public Property IsSubDevice As Boolean
Public Property Level As Integer
Public Property LevelInt As Integer
Public Property MaxDimLevel As Integer
Public Property StrParam1 As String
Public Property StrParam2 As String
Public Property SwitchType As String
End Class
要解析/访问的代码如下:
Public Function ListOfDevices(Optional ByRef DeviceType As String = "all") As DomoticzDevices_Rootobject
Dim result As New DomoticzDevices_Rootobject
Dim resp As HttpWebResponse
Dim PathToService As String = _EndPointURL & "/json.htm?type=devices&filter=" + DeviceType + "&used=true&order=Name"
Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create(PathToService), HttpWebRequest)
req.Accept = "application/json"
req.Method = "GET"
req.PreAuthenticate = True
Try
resp = TryCast(req.GetResponse(), HttpWebResponse)
Dim resultAPI As New CallRestOutput
If (Not resp Is Nothing) Then
Using responseStream As New IO.StreamReader(resp.GetResponseStream())
If (Not responseStream Is Nothing) Then
resultAPI.Output = responseStream.ReadToEnd()
End If
End Using
Try
JsonConvert.PopulateObject(resultAPI.Output, result)
Catch ex As Exception
Throw
End Try
End If
Catch ex As Exception
Throw
End Try
Return result
End Function
正在发生的是,DomoticzDevices_Rootobject
中除result
,status
和title
以外的所有项目均已填充。我不确定为什么不是。根响应中的项目是系统统计信息。我真正需要的是result
数组中的项目,我将在前端使用它们。
我想念什么?