簡體   English   中英

在 vb 中反序列化 JSON 時出錯,為什么我會收到異常詳細信息:System.ArgumentException:傳入的無效數組,“,”預期?

[英]Error Deserializing JSON in vb, Why am I getting Exception Details: System.ArgumentException: Invalid array passed in, ',' expected?

我是使用 JSON 和 VB.net 的新手。 我正在嘗試反序列化以下 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

作為參考,我的多邊形 class 是

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

並且坐標 class 是具有緯度、經度屬性的類似 class。 我在網上看到的大多數示例和教程都有不同的 JSON 結構,因此很難比較。 任何有關我哪里出錯的見解將不勝感激。 謝謝。

使用 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 ]
        ]
    }
]}

解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM