You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET如何从类JSON字符串提取ProductName和Quantity到DataTable

在VB.NET中从类JSON字符串提取指定字段到DataTable

Hey,我来帮你搞定这个问题!首先注意你提供的JSON片段是两个独立的对象,咱们得先把它们包裹成一个JSON数组(用[]括起来),这样才能被正确解析成集合。接下来我会用两种常见的方式实现:最流行的第三方库Json.NET,以及.NET自带的System.Text.Json。


方法一:使用Json.NET(Newtonsoft.Json)

这是VB.NET生态里处理JSON最常用的工具,上手简单功能强。

步骤1:修正JSON格式

把你的原始字符串改成数组格式(否则解析会报错):

[
  { "ProductGroupId": "3", "ProductGroupName": "Frisdranken", "ProductId": "139", "ProductName": "Cola", "Quantity": 1, "QuantityUnit": "P", "SellingPrice": 2.7, "VatRateId": "A", "DiscountLines": [] },
  { "ProductGroupId": "3", "ProductGroupName": "Frisdranken", "ProductId": "146", "ProductName": "Plat water", "Quantity": 1, "QuantityUnit": "P", "SellingPrice": 2.6, "VatRateId": "A", "DiscountLines": [] }
]

步骤2:安装Json.NET

打开Visual Studio,右键你的项目 → 管理NuGet程序包,搜索Newtonsoft.Json并安装最新版本。

步骤3:编写代码实现提取

Imports Newtonsoft.Json
Imports System.Data

Module Program
    Sub Main()
        ' 修正后的JSON字符串
        Dim jsonString As String = "[{""ProductGroupId"": ""3"", ""ProductGroupName"": ""Frisdranken"", ""ProductId"": ""139"", ""ProductName"": ""Cola"", ""Quantity"": 1, ""QuantityUnit"": ""P"", ""SellingPrice"": 2.7, ""VatRateId"": ""A"", ""DiscountLines"": [] }, {""ProductGroupId"": ""3"", ""ProductGroupName"": ""Frisdranken"", ""ProductId"": ""146"", ""ProductName"": ""Plat water"", ""Quantity"": 1, ""QuantityUnit"": ""P"", ""SellingPrice"": 2.6, ""VatRateId"": ""A"", ""DiscountLines"": [] }]"

        ' 创建目标DataTable,定义需要的列
        Dim targetDt As New DataTable()
        targetDt.Columns.Add("ProductName", GetType(String))
        targetDt.Columns.Add("Quantity", GetType(Integer))

        ' 解析JSON为类型安全的实体列表(推荐,避免动态类型的潜在问题)
        Dim products = JsonConvert.DeserializeObject(Of List(Of Product))(jsonString)

        ' 遍历提取字段并添加到DataTable
        For Each prod In products
            Dim newRow = targetDt.NewRow()
            newRow("ProductName") = prod.ProductName
            newRow("Quantity") = prod.Quantity
            targetDt.Rows.Add(newRow)
        Next

        ' 验证结果(可选)
        For Each row In targetDt.Rows
            Console.WriteLine($"产品:{row("ProductName")} | 数量:{row("Quantity")}")
        Next
    End Sub

    ' 定义对应JSON结构的实体类(只保留需要的字段也可以)
    Public Class Product
        Public Property ProductName As String
        Public Property Quantity As Integer
    End Class
End Module

如果想快速实现,也可以用动态对象省略实体类,但类型安全会差一些:

' 替换解析和遍历部分的代码
Dim products = JsonConvert.DeserializeObject(Of List(Of Object))(jsonString)
For Each prod In products
    Dim newRow = targetDt.NewRow()
    newRow("ProductName") = prod.ProductName
    newRow("Quantity") = prod.Quantity
    targetDt.Rows.Add(newRow)
Next

方法二:使用.NET自带的System.Text.Json

如果你不想依赖第三方库,.NET Core 3.0+和.NET 5+自带的System.Text.Json也能搞定。

步骤1:同样先修正JSON格式(和方法一一致)

步骤2:编写代码

Imports System.Text.Json
Imports System.Data

Module Program
    Sub Main()
        Dim jsonString As String = "[{""ProductGroupId"": ""3"", ""ProductGroupName"": ""Frisdranken"", ""ProductId"": ""139"", ""ProductName"": ""Cola"", ""Quantity"": 1, ""QuantityUnit"": ""P"", ""SellingPrice"": 2.7, ""VatRateId"": ""A"", ""DiscountLines"": [] }, {""ProductGroupId"": ""3"", ""ProductGroupName"": ""Frisdranken"", ""ProductId"": ""146"", ""ProductName"": ""Plat water"", ""Quantity"": 1, ""QuantityUnit"": ""P"", ""SellingPrice"": 2.6, ""VatRateId"": ""A"", ""DiscountLines"": [] }]"

        Dim targetDt As New DataTable()
        targetDt.Columns.Add("ProductName", GetType(String))
        targetDt.Columns.Add("Quantity", GetType(Integer))

        ' 配置序列化选项,忽略大小写匹配
        Dim jsonOptions As New JsonSerializerOptions With {.PropertyNameCaseInsensitive = True}
        Dim products = JsonSerializer.Deserialize(Of List(Of Product))(jsonString, jsonOptions)

        For Each prod In products
            Dim newRow = targetDt.NewRow()
            newRow("ProductName") = prod.ProductName
            newRow("Quantity") = prod.Quantity
            targetDt.Rows.Add(newRow)
        Next

        ' 验证输出
        For Each row In targetDt.Rows
            Console.WriteLine($"产品:{row("ProductName")} | 数量:{row("Quantity")}")
        Next
    End Sub

    Public Class Product
        Public Property ProductName As String
        Public Property Quantity As Integer
    End Class
End Module

两种方法最终都会得到包含ProductName和Quantity字段的DataTable,你可以根据项目依赖情况选择合适的方案。

内容的提问来源于stack exchange,提问作者Koen Amant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:13:08