我下面有一些代码,可以从网站上很好地得到响应,我的代码可以一直工作到到达解码位,但不返回任何对象。 这是我第一次尝试JSON和与网络相关的数据。
它可以正常工作,直到: 将对象作为对象 它什么也没返回,但我已经尝试过从网络上选择几种方法,但是很笨拙。
这是JSON网站的网址: https://www.metcheck.com/OTHER/json_data.asp?zipcode=wv12+4qz&locationID=60883&lat=52.6&lon=-2
以下是显示JSON文件的URL: http://ws1.metcheck.com/ENGINE/v9_0/json.asp?lat=52.6&lon=-2&lid=60883&Fc=No
Imports System.IO
Imports System.Net
Imports System.Web.Script.Serialization
Public Class Weather
Private Sub Weather_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim uriString As String = "http://ws1.metcheck.com/ENGINE/v9_0/json.asp?lat=52.6&lon=-2&lid=60883&Fc=No"
Dim uri As New Uri(uriString)
Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
request.Method = "GET"
Dim response As HttpWebResponse = request.GetResponse()
Dim read As New StreamReader(response.GetResponseStream())
Dim raw As String = read.ReadToEnd
Dim dict As Object = New JavaScriptSerializer().Deserialize(Of List(Of Object))(raw)
For Each item As Object In dict
TextBox1.Text = item("temperature").ToString & vbCrLf
TextBox1.Text = item("dewpoint").ToString & vbCrLf
TextBox1.Text = item("rain").ToString & vbCrLf
Next
End Sub
End Class
答案 0 :(得分:0)
我以前没有使用JavaScriptSerializer,但这可能是因为您试图反序列化为List(of object)而不是更具体的类型。如果添加一系列与JSON结构匹配的类:
Public Class Rootobject
Public Property metcheckData As Metcheckdata
Public Property feedCreation As Date
Public Property feedCreator As String
Public Property feedModel As String
Public Property feedModelRun As String
Public Property feedModelRunInitialTime As Date
Public Property feedResolution As String
End Class
Public Class Metcheckdata
Public Property forecastLocation As Forecastlocation
End Class
Public Class Forecastlocation
Public Property forecast As List(Of Forecast)
Public Property continent As String
Public Property country As String
Public Property location As String
Public Property latitude As Single
Public Property longitude As Single
Public Property timezone As Integer
End Class
Public Class Forecast
Public Property temperature As String
Public Property dewpoint As String
Public Property rain As String
Public Property freezinglevel As String
Public Property uvIndex As String
Public Property totalcloud As String
Public Property lowcloud As String
Public Property medcloud As String
Public Property highcloud As String
Public Property humidity As String
Public Property windspeed As String
Public Property meansealevelpressure As String
Public Property windgustspeed As String
Public Property winddirection As String
Public Property windletter As String
Public Property icon As String
Public Property iconName As String
Public Property chanceofrain As String
Public Property chanceofsnow As String
Public Property dayOfWeek As String
Public Property weekday As String
Public Property sunrise As String
Public Property sunset As String
Public Property dayOrNight As String
Public Property utcTime As Date
End Class
然后尝试如下进行反序列化:
Dim dict As Object = New JavaScriptSerializer().Deserialize(Of Rootobject)(raw)
能按预期工作吗?
答案 1 :(得分:0)
这里的真正问题是,您试图反序列化为List,但是JSON实际上代表一个包含data属性的单个对象,然后该data属性包含一个对象列表。这就是为什么您会收到此错误。 Json.Net无法将单个对象反序列化为列表。我认为您真正想做的是定义一个容器类
Public Class Metdata
Public Property myDictionary As List(Of Dictionary(Of String, Object))
End Class
然后您的潜艇将如下所示:
Private Sub Weather_Load(sender As Object, e As EventArgs) 'Handles MyBase.Load
Dim uriString As String = "http://ws1.metcheck.com/ENGINE/v9_0/json.asp?lat=52.6&lon=-2&lid=60883&Fc=No"
Dim uri As New Uri(uriString)
Dim request As HttpWebRequest = HttpWebRequest.Create(uri)
request.Method = "GET"
Dim response As HttpWebResponse = request.GetResponse()
Dim read As New StreamReader(response.GetResponseStream())
Dim raw As String = read.ReadToEnd
Dim dict As Metdata
dict = JsonConvert.DeserializeObject(Of Metdata)(raw)
End Sub
Partial Public Class Metdata
Public Property myDictionary As List(Of Dictionary(Of String, Object))
End Class
Public Class Forecast
<JsonProperty("temperature")>
Public Property Temperature As String
<JsonProperty("dewpoint")>
Public Property Dewpoint As String
<JsonProperty("rain")>
Public Property Rain As String
<JsonProperty("freezinglevel")>
Public Property Freezinglevel As String
<JsonProperty("uvIndex")>
Public Property UvIndex As String
<JsonProperty("totalcloud")>
Public Property Totalcloud As String
<JsonProperty("lowcloud")>
Public Property Lowcloud As String
<JsonProperty("medcloud")>
Public Property Medcloud As String
<JsonProperty("highcloud")>
Public Property Highcloud As String
<JsonProperty("humidity")>
Public Property Humidity As String
<JsonProperty("windspeed")>
Public Property Windspeed As String
<JsonProperty("meansealevelpressure")>
Public Property Meansealevelpressure As String
<JsonProperty("windgustspeed")>
Public Property Windgustspeed As String
<JsonProperty("winddirection")>
Public Property Winddirection As String
<JsonProperty("windletter")>
Public Property Windletter As String
<JsonProperty("icon")>
Public Property Icon As String
<JsonProperty("iconName")>
Public Property IconName As String
<JsonProperty("chanceofrain")>
Public Property Chanceofrain As String
<JsonProperty("chanceofsnow")>
Public Property Chanceofsnow As String
<JsonProperty("dayOfWeek")>
Public Property DayOfWeek As String
<JsonProperty("weekday")>
Public Property Weekday As String
<JsonProperty("sunrise")>
Public Property Sunrise As String
<JsonProperty("sunset")>
Public Property Sunset As String
<JsonProperty("dayOrNight")>
Public Property DayOrNight As String
<JsonProperty("utcTime")>
Public Property UtcTime As DateTime
End Class
Public Class ForecastLocation
<JsonProperty("forecast")>
Public Property Forecast As Forecast()
<JsonProperty("continent")>
Public Property Continent As String
<JsonProperty("country")>
Public Property Country As String
<JsonProperty("location")>
Public Property Location As String
<JsonProperty("latitude")>
Public Property Latitude As Double
<JsonProperty("longitude")>
Public Property Longitude As Double
<JsonProperty("timezone")>
Public Property Timezone As Integer
End Class
Public Class MetcheckData
<JsonProperty("forecastLocation")>
Public Property ForecastLocation As ForecastLocation
End Class
Public Class Metdata
<JsonProperty("metcheckData")>
Public Property MetcheckData As MetcheckData
<JsonProperty("feedCreation")>
Public Property FeedCreation As DateTime
<JsonProperty("feedCreator")>
Public Property FeedCreator As String
<JsonProperty("feedModel")>
Public Property FeedModel As String
<JsonProperty("feedModelRun")>
Public Property FeedModelRun As String
<JsonProperty("feedModelRunInitialTime")>
Public Property FeedModelRunInitialTime As DateTime
<JsonProperty("feedResolution")>
Public Property FeedResolution As String
End Class
dict.Metcheckdata.Forecastlocation.Forecast将包含您的155条记录。