我尝试反序列化这个JSON:
[
{
"trends": [
{
"name": "#GiftAGamer",
"url": "http://twitter.com/search?q=%23GiftAGamer",
"promoted_content": null,
"query": "%23GiftAGamer",
"tweet_volume": null
},
{
"name": "#AskCuppyAnything",
"url": "http://twitter.com/search?q=%23AskCuppyAnything",
"promoted_content": null,
"query": "%23AskCuppyAnything",
"tweet_volume": 14504
}
],
"as_of": "2020-11-20T19:37:52Z",
"created_at": "2020-11-19T14:15:43Z",
"locations": [
{
"name": "Worldwide",
"woeid": 1
}
]
}
]
所以我创建了3个类:
Public Class TwitterTrendApiResponse
Public Property ttd As List(Of TwitterTrendDatas)
Public Property datAsOf As String
Public Property datCreatedAt As String
Public Property ttl As List(Of TwitterTrendLocation)
End Class
Public Class TwitterTrendLocation
Public Property strName As String
Public Property intWoeid As String
End Class
Public Class TwitterTrendDatas
Public Property strName As String
Public Property strUrl As String
Public Property strPromotedContent As String
Public Property strQuery As String
Public Property intVolume As String
End Class
我试过反序列化:
Dim result As TwitterTrendApiResponse = JsonConvert.DeserializeObject(strMyJsonToDeserialize)
但是我遇到了一个异常“无法将类型为”Newtonsoft.json.linq.JArray“的对象转换为TwitterTrendApiResponse类型。我哪里出错了?”
2条答案
按热度按时间gv8xihay1#
事实上,你的json是一个对象列表,而不仅仅是一个对象。
并修正课程
r3i60tvu2#
你必须改变这一点:
在:
正如您在JSON数据中所看到的,根节点是一个数组。然后,如果返回的对象是数组或单个类,请注意其类型,以便正确声明。
AS List(Of TwitterTrendApiResponse)
或AS TwitterTrendApiResponse