【问题标题】:Need helping parsing specific JSON需要帮助解析特定的 JSON
【发布时间】:2016-12-27 18:09:17
【问题描述】:

我有这个 JSON 字符串:

{
    "Store": [{
        "StoreID": "8643",
        "IsDeliveryStore": true,
        "MinDistance": 0.6,
        "MaxDistance": 0.6,
        "Phone": "352-505-5557",
        "AddressDescription": "3581 sw archer rd\nGainesville, FL 32608",
        "HolidaysDescription": "",
        "HoursDescription": "Mo-Fr 9:30am-4:00am\nSa-Su 10:00am-4:00am",
        "ServiceHoursDescription": {
            "Carryout": "Mo-Fr 9:30am-4:00am\nSa-Su 10:00am-4:00am",
            "Delivery": "Mo-Fr 9:30am-4:00am\nSa-Su 10:00am-4:00am"
        },
        "IsOnlineCapable": true,
        "IsOnlineNow": true,
        "IsNEONow": false,
        "IsSpanish": true,
        "LocationInfo": null,
        "LanguageLocationInfo": {
            "es": null
        },
        "AllowDeliveryOrders": true,
        "AllowCarryoutOrders": true,
        "IsOpen": true,
        "ServiceIsOpen": {
            "Carryout": true,
            "Delivery": true
        }
    }, {
        "StoreID": "3214",
        "IsDeliveryStore": false,
        "MinDistance": 2.1,
        "MaxDistance": 2.1,
        "Phone": "352-377-4992",
        "AddressDescription": "3309 W University Ave\nGainesville, FL 32607\nIn Publix Plazza.",
        "HolidaysDescription": "",
        "HoursDescription": "Su-Th 10:00am-3:00am\nFr-Sa 10:00am-4:00am",
        "ServiceHoursDescription": {
            "Carryout": "Su-Th 10:00am-3:00am\nFr-Sa 10:00am-4:00am",
            "Delivery": "Su-Th 10:00am-3:00am\nFr-Sa 10:00am-4:00am"
        },
        "IsOnlineCapable": true,
        "IsOnlineNow": true,
        "IsNEONow": false,
        "IsSpanish": false,
        "LocationInfo": "In Publix Plazza.",
        "LanguageLocationInfo": {
            "es": "In Publix Plazza."
        },
        "AllowDeliveryOrders": true,
        "AllowCarryoutOrders": true,
        "IsOpen": true,
        "ServiceIsOpen": {
            "Carryout": true,
            "Delivery": true
        }
    }, {
        "StoreID": "3172",
        "IsDeliveryStore": false,
        "MinDistance": 2.7,
        "MaxDistance": 2.7,
        "Phone": "352-373-8888",
        "AddressDescription": "5750 SW 75th Ct #40\nGainesville, FL 32608\nIn Park Lane Plaza  .",
        "HolidaysDescription": "",
        "HoursDescription": "Su-Th 10:00am-1:00am\nFr-Sa 10:00am-2:00am",
        "ServiceHoursDescription": {
            "Carryout": "Su-Th 10:00am-1:00am\nFr-Sa 10:00am-2:00am",
            "Delivery": "Su-Th 10:00am-1:00am\nFr-Sa 10:00am-2:00am"
        },
        "IsOnlineCapable": true,
        "IsOnlineNow": true,
        "IsNEONow": false,
        "IsSpanish": true,
        "LocationInfo": "In Park Lane Plaza  .",
        "LanguageLocationInfo": {
            "es": "In Park Lane Plaza  ."
        },
        "AllowDeliveryOrders": true,
        "AllowCarryoutOrders": true,
        "IsOpen": true,
        "ServiceIsOpen": {
            "Carryout": true,
            "Delivery": true
        }
    }, {
        "StoreID": "5150",
        "IsDeliveryStore": false,
        "MinDistance": 2.9,
        "MaxDistance": 2.9,
        "Phone": "352-373-2337",
        "AddressDescription": "2106 Sw 13th St\nGainesville, FL 32608\nIn Domino's Plaza.",
        "HolidaysDescription": "",
        "HoursDescription": "Mo-Fr 9:30am-4:00am\nSa-Su 10:00am-4:00am",
        "ServiceHoursDescription": {
            "Carryout": "Mo-Fr 9:30am-4:00am\nSa-Su 10:00am-4:00am",
            "Delivery": "Mo-Fr 9:30am-4:00am\nSa-Su 10:00am-4:00am"
        },
        "IsOnlineCapable": true,
        "IsOnlineNow": false,
        "IsNEONow": false,
        "IsSpanish": false,
        "SubstitutionStore": "3213",
        "LocationInfo": "In Domino's Plaza.",
        "LanguageLocationInfo": {
            "es": "In Domino's Plaza."
        },
        "AllowDeliveryOrders": true,
        "AllowCarryoutOrders": true,
        "IsOpen": true,
        "ServiceIsOpen": {
            "Carryout": true,
            "Delivery": true
        }
    }, {
        "StoreID": "3246",
        "IsDeliveryStore": false,
        "MinDistance": 4.5,
        "MaxDistance": 4.5,
        "Phone": "352-692-2222",
        "AddressDescription": "4620 NW 39th Ave\nGainesville, FL\nLocated at Magnolia Park.",
        "HolidaysDescription": "",
        "HoursDescription": "Su-Th 10:00am-12:00am\nFr-Sa 10:00am-1:30am",
        "ServiceHoursDescription": {
            "Carryout": "Su-Th 10:00am-12:00am\nFr-Sa 10:00am-1:30am",
            "Delivery": "Su-Th 10:00am-12:00am\nFr-Sa 10:00am-1:30am"
        },
        "IsOnlineCapable": true,
        "IsOnlineNow": true,
        "IsNEONow": false,
        "IsSpanish": true,
        "LocationInfo": "Located at Magnolia Park.",
        "LanguageLocationInfo": {
            "es": "Located at Magnolia Park."
        },
        "AllowDeliveryOrders": true,
        "AllowCarryoutOrders": true,
        "IsOpen": true,
        "ServiceIsOpen": {
            "Carryout": true,
            "Delivery": true
        }
    }, {
        "StoreID": "3261",
        "IsDeliveryStore": false,
        "MinDistance": 4.7,
        "MaxDistance": 4.7,
        "Phone": "352-505-6845",
        "AddressDescription": "3000 NW 83rd Street\nGainesville, FL 32606\nLocated at the Food Court of Santa Fe College (Bldg R).",
        "HolidaysDescription": "",
        "HoursDescription": "Mo-Fr 8:30am-5:00pm\nSa-Su Closed",
        "ServiceHoursDescription": {
            "Carryout": "Mo-Fr 8:30am-5:00pm\nSa-Su Closed",
            "Delivery": "Mo-Fr 8:30am-5:00pm\nSa-Su Closed"
        },
        "IsOnlineCapable": true,
        "IsOnlineNow": true,
        "IsNEONow": false,
        "IsSpanish": true,
        "LocationInfo": "Located at the Food Court of Santa Fe College (Bldg R).",
        "LanguageLocationInfo": {
            "es": "Located at the Food Court of Santa Fe College (Bldg R)."
        },
        "AllowDeliveryOrders": true,
        "AllowCarryoutOrders": true,
        "IsOpen": true,
        "ServiceIsOpen": {
            "Carryout": true,
            "Delivery": true
        }
    }, {
        "StoreID": "3213",
        "IsDeliveryStore": false,
        "MinDistance": 4.7,
        "MaxDistance": 4.7,
        "Phone": "352-373-5555",
        "AddressDescription": "25 Nw 16th Ave\nGainesville, FL 32601\nOn the north corner of Main Street and NW 16th avenue.",
        "HolidaysDescription": "",
        "HoursDescription": "Su-Th 10:00am-3:00am\nFr-Sa 10:00am-4:00am",
        "ServiceHoursDescription": {
            "Carryout": "Su-Th 10:00am-3:00am\nFr-Sa 10:00am-4:00am",
            "Delivery": "Su-Th 10:00am-3:00am\nFr-Sa 10:00am-4:00am"
        },
        "IsOnlineCapable": true,
        "IsOnlineNow": true,
        "IsNEONow": false,
        "IsSpanish": false,
        "LocationInfo": "On the north corner of Main Street and NW 16th avenue.",
        "LanguageLocationInfo": {
            "es": "On the north corner of Main Street and NW 16th avenue."
        },
        "AllowDeliveryOrders": true,
        "AllowCarryoutOrders": true,
        "IsOpen": true,
        "ServiceIsOpen": {
            "Carryout": true,
            "Delivery": true
        }
    }]
}

我正在尝试解析这个,这是我的代码:

Dim StoreWrapper = JsonConvert.DeserializeObject(Of StoreWrapper)(html)
Dim AllStores = StoreWrapper.Stores
Results.Text = AllStores(1).StoreID.ToString

Public Class StoreWrapper
    Public Property Store As List(Of Store)
End Class
Public Class Store
Public Property StoreID As String
Public Property IsDeliveryStore As Boolean
Public Property MinDistance As Double
Public Property MaxDistance As Double
Public Property Phone As String
Public Property AddressDescription As String
Public Property HolidaysDescription As String
Public Property HoursDescription As String
'Public Property ServiceHoursDescription As String
Public Property IsOnlineCapable As Boolean
Public Property IsOnlineNow As Boolean
Public Property IsNEONow As Boolean
Public Property IsSpanish As Boolean
Public Property LocationInfo As String
'Public Property LanguageLocationInfo As String
Public Property AllowDeliveryOrders As Boolean
Public Property AllowCarryoutOrders As Boolean
Public Property IsOpen As Boolean
'Public Property ServiceIsOpen As String
End Class

我得到的错误是:

LocationFinder.exe 中出现“System.NullReferenceException”类型的未处理异常

附加信息:对象引用未设置为对象的实例。

【问题讨论】:

  • 请阅读How to Ask 并使用tour,我们需要知道它发生在哪一行。还要花时间学习如何格式化代码,使其看起来像代码
  • 您的课程不太正确。不是 evenrything 是字符串,有一些布尔值(true 是布尔值,"true" 是字符串)并在其中加倍;看起来您还缺少一些类型:ServiceHoursDescriptionLanguageLocationInfoServiceIsOpen 是它们自己的类型
  • 注意:最终结果是 NRE,但 that 是 Store 类不正确的结果,因此 StoreWrapper 通常为 Nothing。该代码不会检查它是否已反序列化,但规范上并没有太多可以帮助 OP 实际解决潜在问题的内容。不幸的是,她/他只是把他们的问题像扔了这么多脏衣服一样扔掉了,然后逃离了现场;所以如果它作为一个骗子关闭,他们不能真正责怪任何人,除了他们自己。
  • 谢谢。发生错误的行是Results.Text = AllStores(1).StoreID.ToString 我确实更改了类型以匹配,我省略了需要类型的那些,因为我真的不需要那些。我不必从 JSON 中读取每个令牌,这是否正确?

标签: json vb.net json.net


【解决方案1】:

需要的改变是 Public Property Store As List(Of Store)

谢谢!

【讨论】:

    猜你喜欢
    • 2016-07-14
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多