【问题标题】:Unable to Parse odd JSON Object with JsonConvert无法使用 JsonConvert 解析奇怪的 JSON 对象
【发布时间】:2018-02-04 18:13:59
【问题描述】:

无法使用 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 对象反序列化的数组或列表。 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 列表...

For Each jobject In dataWrapper.ico.upcoming
   Dim s As String = jobject.name
Next

【问题讨论】:

标签: asp.net vb.net json.net


【解决方案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

注意事项:

  • 您需要在根对象MarketWrapperList(Of Upcoming) 之间增加一个额外级别的Ico 类来捕获upcoming 属性。

  • description 必须是 String 而不是 Single

  • 我确实将自动生成的Public Property upcoming As Upcoming() 更改为List(Of Upcoming) 类型。像这样的代码生成工具通常更喜欢数组而不是列表。

示例VB.NET fiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多