【问题标题】:Deserializing of collection with nested array in VB.NET and Newtonsoft在 VB.NET 和 Newtonsoft 中使用嵌套数组反序列化集合
【发布时间】:2021-02-10 03:06:20
【问题描述】:

我想使用 Newtonsoft 反序列化集合将 JSON 引号字符串转换为引号列表。

我检查了herehere

我在尝试转换时遇到错误:

无法从 System.String 转换或转换为 System.Collections.Generic.List`1[System.String]。

然后我尝试将图像属性更改为:

Private _images As String()
Public Property images() As String()
    Get
        Return _images
    End Get
    Set(ByVal value As String())
        _images = value
    End Set
End Property

然后我得到错误:

无法从 System.String 转换或转换为 System.String[]。

如何将我的 JSON 转换为我的类,这样我不仅拥有作者和引号字符串属性,而且还有图像数组作为强类型?

Public Class quote
    Private _author As String
    Public Property author() As String
        Get
            Return _author
        End Get
        Set(ByVal value As String)
            _author = value
        End Set
    End Property

    Private _quote As String
    Public Property quote() As String
        Get
            Return _quote
        End Get
        Set(ByVal value As String)
            _quote = value
        End Set
    End Property
End Class

Dim quotesString As String = "[{""author"": ""J.K. Rowling"",""quote"": ""\u201cThe truth.\"" Dumbledore sighed. \""It is a beautiful and terrible thing, and should therefore be treated with great caution.\u201d"",""images"": [""dumbledore1.jpg"", ""dumbledore2.jpg"", ""dumbledore3.jpg""]}, {""author"": ""Jimi Hendrix"",""quote"": ""\u201cI'm the one that's got to die when it's time for me to die, so let me live my life the way I want to.\u201d"",""images"": [""Jimi.jpg"",""Jimi3.jpg"",""Jimi2.jpg""]},{""author"": ""J.M. Barrie"",""quote"": ""\u201cTo die will be an awfully big adventure.\u201d"",""images"": [""Barrie.jpg"",""Barrie1.jpg"",""Barrie2.jpg""]}]"

Dim quotes As List(Of quote) = Newtonsoft.Json.JsonConvert.DeserializeObject(Of List(Of quote))(quotesString)

JSON 源

[{
        "author": "J.K. Rowling",
        "quote": "\u201cThe truth.\" Dumbledore sighed. \"It is a beautiful and terrible thing, and should therefore be treated with great caution.\u201d",
        "images": ["dumbledore1.jpg", "dumbledore2.jpg", "dumbledore3.jpg"]
    }, {
        "author": "Jimi Hendrix",
        "quote": "\u201cI'm the one that's got to die when it's time for me to die, so let me live my life the way I want to.\u201d",
        "images": ["Jimi.jpg","Jimi3.jpg","Jimi2.jpg"]
    },
    {
        "author": "J.M. Barrie",
        "quote": "\u201cTo die will be an awfully big adventure.\u201d",
        "images": ["Barrie.jpg","Barrie1.jpg","Barrie2.jpg"]
    }

]   

【问题讨论】:

  • images 属性的内容不是有效的 JSON,因此您不能以这种方式对其进行反序列化。您需要使用正则表达式(或 String.Split 在紧要关头)提取值。
  • @Dai 谢谢,我自己控制 JSON 源,所以我现在更新它。你能帮忙吗?
  • 另外,您使用 VB.NET 有什么原因吗?
  • 是的,正在处理遗留项目
  • 你有我的同情。他们强迫你使用什么版本的 VB.NET?

标签: arrays json vb.net class json.net


【解决方案1】:
  • 将您的属性更改为List<String>(在VB.NET 中为List(Of String))。
    • 虽然String[](VB.NET 中的String()应该工作,但我个人在将 JSON 反序列化为 List<String> 时遇到的问题较少 - 有时计算机很诡异。
  • 您还可以将 VB.NET 类定义大大简化为 use Auto-Implemented Properties:不需要显式字段和样板的 getter/setter 逻辑。
  • 此外,在 .NET 中:公共成员(属性、方法)应为 PascalCase,类型名称也应如此。
    • 如果它们与 .NET 类成员名称不完全匹配(Newtonsoft.Json 默认情况下区分大小写),您可以使用 JsonProperty 属性来指定显式 JSON 属性名称。
      • 另一种方法是配置CamelCasePropertyNamesContractResolver,但我个人更喜欢显式的[JsonProperty] 属性。
Public Class Quote

    <JsonProperty("author")>
    Public Property Author As String

    <JsonProperty("quote")>
    Public Property Quote As String

    <JsonProperty("images")>
    Public Property Images As List(Of String)

End Class

这是它在 Linqpad 中运行的屏幕截图:

【讨论】:

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