2

我在 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() 创建一个无参数的构造函数?

谢谢参观!

4

2 回答 2

2

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

于 2013-03-15T08:12:28.633 回答
1

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

Class something
  Public property kind as string = String.Empty

  Public property items as list(of Item) = Nothing
End Class

然后你反序列化为something,而不是 list(of item)

于 2013-03-15T08:02:38.003 回答