我意识到有很多关于使用JSON的问题,但我还没有找到任何有助于解决问题的方法。
总结:
我当前的反序列化代码是这样的,其中wrString是我的json
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer
Dim rawdata = js.DeserializeObject(wrString)
Dim recs As List( Of TaxonObservation) = CType(rawdata, List(Of TaxonObservation))
MsgBox(recs(0).siteName)
但这会引发错误
unable to cast object of type 'System.Object' to type 'System.Collections.Generic.List'
我认为json是一个对象数组(但这可能是我的错误?)。看起来像这样
"[{"observationID":260810677,"fullVersion":false,"datasetKey":"GA000144","surveyKey":"772","sampleKey":"772-SAMPLE","observationKey":"8048603","siteKey":"NBN-SITE-GA000144-18338220","siteName":"Court Hey Park","featureID":1410645,"location":"SJ417900","resolution":"100m","taxonVersionKey":"NBNSYS0000022280","pTaxonVersionKey":"NBNSYS0000022280","pTaxonName":"Rhytidiadelphus squarrosus","pTaxonAuthority":"(Hedw.) Warnst.","startDate":"2010-09-06","endDate":"2010-09-06","recorder":"Deed, B.","sensitive":false,"absence":false,"publicAttribute":false,"dateTypekey":"D "},{"observationID":260830853,"fullVersion":false,"datasetKey":"GA000144","surveyKey":"772","sampleKey":"772-SAMPLE","observationKey":"8048619","siteKey":"NBN-SITE-GA000144-18338223","siteName":"Court Hey Park","featureID":1410645,"location":"SJ417900","resolution":"100m","taxonVersionKey":"NBNSYS0000036189","pTaxonVersionKey":"NBNSYS0000036189","pTaxonName":"Ceratodon purpureus","pTaxonAuthority":"(Hedw.) Brid.","startDate":"2010-09-30","endDate":"2010-09-30","recorder":"Deed, B.","sensitive":false,"absence":false,"publicAttribute":false,"dateTypekey":"D "}]"
我的课是这个
Public Class TaxonObservation
Public Property observationID As Integer
Public Property fullVersion As Boolean
Public Property datasetKey As String
Public Property surveyKey As String
Public Property sampleKey As String
Public Property observationKey As String
Public Property siteKey As String
Public Property siteName As String
Public Property featureID As Integer
Public Property location As String
Public Property resolution As String
Public Property taxonVersionKey As String
Public Property pTaxonVersionKey As String
Public Property pTaxonName As String
Public Property pTaxonAuthority As String
Public Property startDate As String
Public Property endDate As String
Public Property recorder As String
Public Property sensitive As Boolean
Public Property absence As Boolean
Public Property dateTypekey As String
End Class
答案 0 :(得分:0)
您应该使用通用Deserialize(Of T)
方法而不是DeserializeObject
:
Dim js As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim recs As List(Of TaxonObservation) = js.Deserialize(Of List(Of TaxonObservation))(wrString)
MsgBox(recs(0).siteName)