无法使用JsonConvert解析以下内容......
{
"ico": {
"upcoming": [
{
"name": "Crowdwiz",
"image": "https://icowatchlist.com/logos/crowdwiz.png",
"description": "CrowdWiz Bringing the future of investment into your hands",
"website_link": "https://api.icowatchlist.com/public/v1/url/crowdwiz",
"icowatchlist_url": "https://icowatchlist.com/ico/crowdwiz",
"start_time": "2017-10-24 12:00:00",
"end_time": "2017-11-07 12:00:00",
"timezone": "UTC+0"
},
{
"name": "Publica",
"image": "https://icowatchlist.com/logos/publica.png",
"description": "Blockchain revolution for the publishing economy",
"website_link": "https://api.icowatchlist.com/public/v1/url/publica",
"icowatchlist_url": "https://icowatchlist.com/ico/publica",
"start_time": "2017-10-25 00:00:00",
"end_time": "2017-11-15 00:00:00",
"timezone": "UTC+3"
}
]
}
}
通常我会创建一个类;但无法弄清楚这种结构如何。这是我的代码(vb.net,但C#解决方案很好)。任何帮助将不胜感激......
Try
url = "https://api.icowatchlist.com/public/v1/upcoming"
Dim theurl As New Uri(url)
Using webClient = New System.Net.WebClient()
json = webClient.DownloadString(theurl)
Dim dataWrapper = JsonConvert.DeserializeObject(Of MarketWrapper)(json)
For Each jobject In dataWrapper.ico
Dim s As String = jobject.name
Next
End Using
Catch ex As Exception
some_msg = ex.Message
End Try
我从asp.net得到的错误...
无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Collections.Generic.List`1 [ping60 + DataWrapper]',因为该类型需要JSON数组(例如[1] ,2,3])正确反序列化。要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似的集合类型可以从JSON对象反序列化的数组或List。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。路径'ico.upcoming',第1行,第19位。
我所拥有的课程,尝试了其他选项,但仍然没有...
Class MarketWrapper
Property ico As List(Of DataWrapper)
End Class
Class DataWrapper
Property name As String
Property image As String
Property description As Single
Property website_link As String
Property icowatchlist_url As String
Property start_time As String
Property end_time As String
Property timezone As String
End Class
@dbc解决方案有效。将此添加到我的代码中以循环通过ico list ...
For Each jobject In dataWrapper.ico.upcoming
Dim s As String = jobject.name
Next
答案 0 :(得分:1)
您可以使用代码生成工具(如https://jsonutils.com/或Paste JSON as Classes)从JSON自动生成VB.NET类。像这样的工具不能正确处理所有内容(例如字典),但在这种情况下,jsonutils可以正常工作并生成以下内容:
Public Class Upcoming
Public Property name As String
Public Property image As String
Public Property description As String
Public Property website_link As String
Public Property icowatchlist_url As String
Public Property start_time As String
Public Property end_time As String
Public Property timezone As String
End Class
Public Class Ico
Public Property upcoming As List(Of Upcoming)
End Class
Public Class MarketWrapper
Public Property ico As Ico
End Class
注意:
您需要在根对象Ico
和MarketWrapper
之间增加一级List(Of Upcoming)
来捕获upcoming
属性。
description
必须是String
而不是Single
。
我确实将自动生成的Public Property upcoming As Upcoming()
更改为List(Of Upcoming)
类型。像这样的代码生成工具通常支持列表上的数组。