gpt4 book ai didi

asp.net - 使用 VB 在 ASP.NET 中反序列化 JSON

转载 作者:行者123 更新时间:2023-12-04 05:06:54 25 4
gpt4 key购买 nike

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

问题是我下面的代码返回空值。我想为我声明的每个类(class)成员获取 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() 创建一个无参数的构造函数?

感谢您的观看!

最佳答案

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

Class something
Public property kind as string = String.Empty

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

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

关于asp.net - 使用 VB 在 ASP.NET 中反序列化 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15427100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com