【问题标题】:Deserialize JSON in ASP.NET with VB使用 VB 在 ASP.NET 中反序列化 JSON
【发布时间】:2013-03-03 20:30:18
【问题描述】:

我在 ASP.NET 中使用 VB,我一直在尝试反序列化下面的 JSON 大约 4 天,但没有成功。

问题是我下面的代码返回空值。我想获得我声明的每个班级成员的 JSON 结果,包括每个产品的价格和运费值。

谁能指出我正确的方向

1.) 为什么我不断返回空值和

2.) 如果我声明的类对我试图反序列化的 json 有效?

非常感谢任何帮助!

这是我正在使用的 JSON 示例:

{
 "kind": "shopping#products",
 "items": 
   [{
   "kind": "shopping#product",
   "id": "tag:google.com,2010:shopping/products/8040/8382012077897342942",
   "product": {
    "googleId": "8382012077897342942",
    "title": "LEGO Star Wars™: Jabba's Palace™ (9516)",
    "description": "Rescue Han Solo from Jabba the Hutt's desert palace!",
    "inventories": [
     {
      "price": 119.99,
      "shipping": 12.95,
      "currency": "USD"
     }
    ]
   }
  }
 ]
}

这是我目前拥有的代码:

Imports System.Web.Script.Serialization
Imports Newtonsoft.Json.Linq

Public Class inventories
    Public Property price As Double
    Public Property shipping As Double
End Class

Public Class product
    Public Property googleid As String
    Public Property title As String
    Public Inventories As inventories()
End Class

Public Class Items
    Public Property product As product()
    Public Property kind As String
    Public Property id As String
End Class


Partial Class JSON_Test
    Inherits System.Web.UI.Page

    Protected Sub getbutton_Click(sender As Object, e As System.EventArgs) Handles getbutton.Click

        ' 1. Get JSON string from Google
        Dim google_json_string As String
        google_json_string = json_text.Text


        '2. Deserialize JSON - Method 1
        Dim jss As New JavaScriptSerializer
        Dim ds_results = jss.Deserialize(Of List(Of Items))(google_json_string)
        result1.Text = ds_results.Count
        result2.Text = ds_results(0).kind

    End Sub
End Class

(更新)我更新了代码以包含结构如下的类(感谢 Dan-o):

Public Class g_JSON
    Public Property items As List(Of Items)
End Class

Public Class Items
    Public Property product As product()
    Public Property kind As String
    Public Property id As String
End Class

Public Class product
    Public Property googleid As String
    Public Property title As String
    Public Inventories As inventories()
End Class

Public Class inventories
    Public Property price As Double
    Public Property shipping As Double
End Class

我还更新了代码如下:

Partial Class JSON_Test
    Inherits System.Web.UI.Page

    Protected Sub getbutton_Click(sender As Object, e As System.EventArgs) Handles getbutton.Click

        ' 1. Get JSON string from Google
        Dim google_json_string As String
        google_json_string = json_text.Text


        '2. Deserialize JSON - Method 1
        Dim jss As New JavaScriptSerializer
        Dim ds_results = jss.Deserialize(Of g_JSON)(google_json_string)
        result1.Text = ds_results.items(0).id
        result2.Text = ds_results.items(0).kind

    End Sub
End Class

现在,代码将毫无问题地编译,但是当我启动点击事件时,我收到以下错误:

没有为“product[]”类型定义无参数构造函数。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.MissingMethodException:没有为“product[]”类型定义无参数构造函数。

来源错误: 第 38 行:Dim ds_results = jss.Deserialize(Of g_JSON)(google_json_string)

这是什么意思?如何为 product() 创建无参数构造函数?

感谢观看!

【问题讨论】:

    标签: asp.net vb.net json deserialization


    【解决方案1】:

    根据jsonlint,您的 json 无效(“库存”数组后的逗号)。

    【讨论】:

    • 好地方!你是用工具找到的吗?
    • 是的,评论里有链接。
    • 是的,非常感谢您的回答。我将原始 JSON 从 85 项减少到 1 项,以将示例粘贴到此问题中,但忘记删除额外的逗号。我已经用删除的逗号更新了文本。另外,感谢您对验证 JSON 的网站的引用!
    【解决方案2】:

    你忘记了容器……容纳一切的类……

    Class something
      Public property kind as string = String.Empty
    
      Public property items as list(of Item) = Nothing
    End Class
    

    然后你反序列化为something,而不是列表(项目)

    【讨论】:

    • Dan-o,这是一个很好的建议,我已经在类和代码中实现了它。
    • Dan-o,感谢您抽出宝贵时间回答。这是一个很好的建议,我已经在上面编辑的 cmets 中看到的类和代码中实现了它。现在,当我使用按钮启动代码时出现以下错误:“没有为'product []'类型定义无参数构造函数”。这是什么意思?至少我走得更远了(我认为?)。
    • 正是错误所说的。反序列化程序正在为您的产品数组 (Items.product) 寻找无参数构造函数。您需要将您的数组更改为一个 List(Of Product),它有一个无参数的 ctor。
    • 感谢您的帮助!这有效地消除了错误,我正在努力。
    猜你喜欢
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多