首页 > 解决方案 > 在 vb 中反序列化 JSON 时出错,为什么我会收到异常详细信息:System.ArgumentException:传入的数组无效,预期为“,”?

问题描述

我是在 VB.net 中使用 JSON 的新手。我正在尝试反序列化以下 json。

{
"Polygon": [
    [
        "regionName": "North Coast",
        "coordinates": [
            [ -58209.320199999958277, 441960.463400000706315 ], 
            [ -58162.889899998903275, 441929.368699999526143 ]
        ]
    ],
    [
        "regionName": "East Coast",
        "coordinates": [
            [ -58209.320199999958277, 441960.463400000706315 ], 
            [ -58162.889899998903275, 441929.368699999526143 ]
        ]
    ]
]}

但是,在反序列化行上运行代码时,我收到一条错误消息,指出“异常详细信息:System.ArgumentException:传入的无效数组,','预期”

Dim allRegions() As Polygon
Dim ser As JavaScriptSerializer = New JavaScriptSerializer()

Public Function initialize() As Boolean
    allRegions = New Polygon(2) {}
    Dim path As String = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "BLL\ShapeGisClasses\testing.json")
    Dim jsonString As String = File.ReadAllText(path)
    allRegions = ser.Deserialize(Of Polygon())(jsonString)

    Return True
End Function

作为参考,我的多边形类是

Public Class Polygon
     Public Property regionName As String
     Public Property coordinates() As Coordinate
End Class

坐标类是具有 lat,lon 属性的类似类。我在网上看到的大多数示例和教程都有不同的 JSON 结构,因此很难比较。任何有关我哪里出错的见解将不胜感激。谢谢。

标签: jsonvb.netdeserialization

解决方案


使用 Jimi 的建议,我将我的 JSON 调整为

{
"regions": [
    {
        "regionName": "North Coast",
        "coordinates": [
            [ -58209.320199999958277, 441960.463400000706315 ],
            [ -58162.889899998903275, 441929.368699999526143 ]
        ]
    },
    {
        "regionName": "East Coast",
        "coordinates": [
            [ -58209.320199999958277, 441960.463400000706315 ],
            [ -58162.889899998903275, 441929.368699999526143 ]
        ]
    }
]}

解决了我的问题。


推荐阅读