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

如何将JSON中billItemList字段格式化为带[]的数组结构

问题:将VB.NET生成的JSON中单个对象字段改为数组格式

现有生成的JSON

{"billNumber":"0003","entityActivityId":572,"customerFullName":"التوكيلات التجارية","customerMobileNumber":"0506436506","customerPreviousBalance":0,"issueDate":"2022-08-16","expireDate":"2022-09-23","billItemList":{"name":"*","quantity":1,"unitPrice":900,"discount":0,"discountType":"FIXED","vat":"0.15"}}

期望的JSON格式

{"billNumber":"0003","entityActivityId":572,"customerFullName":"التوكيلات التجارية","customerMobileNumber":"0506436506","customerPreviousBalance":0,"issueDate":"2022-08-16","expireDate":"2022-09-23","billItemList":[{"name":"*","quantity":1,"unitPrice":900,"discount":0,"discountType":"FIXED","vat":"0.15"}]}

当前代码实现

dt1 = dt.ToString("yyyy-MM-dd")
dt2 = dt.AddDays(7).ToString("yyyy-MM-dd")
Dim b As New billItemList With
{
    .name = "*",
    .quantity = 1,
    .unitPrice = txtSubTotal.Value,
    .discount = txtDiscountValue.Value,
    .discountType = "FIXED",
    .vat = 0.15
}

Dim person = New EfaaAPI() With {
    .billNumber = txtInvNo.Value,
    .entityActivityId = 572,
    .customerFullName = CN,
    .customerMobileNumber = txtTelNo.Value,
    .issueDate = dt1,
    .expireDate = dt2,
    .billItemList = b
}

Dim json = JsonConvert.SerializeObject(person)

当前类定义

Public Class EfaaAPI
    Public Property billNumber As String 'optional
    Public Property entityActivityId As Integer
    Public Property customerFullName As String
    Public Property customerMobileNumber As String
    Public Property customerPreviousBalance As Integer
    Public Property issueDate As String  'YYYY-MM-DD
    Public Property expireDate As String 'YYYY-MM-DD
    Public Property billItemList As New billItemList()
End Class

Public Class billItemList
    Public Property name As String
    Public Property quantity As Integer
    Public Property unitPrice As Integer
    Public Property discount As Integer
    Public Property discountType As String
    Public Property vat As String
End Class

修改方案

1. 调整EfaaAPI类的billItemList属性类型

将单个billItemList对象类型改为泛型列表,这样Json.NET会自动序列化为数组格式:

Public Class EfaaAPI
    Public Property billNumber As String 'optional
    Public Property entityActivityId As Integer
    Public Property customerFullName As String
    Public Property customerMobileNumber As String
    Public Property customerPreviousBalance As Integer
    Public Property issueDate As String  'YYYY-MM-DD
    Public Property expireDate As String 'YYYY-MM-DD
    ' 修改为List(Of billItemList)类型并初始化空列表
    Public Property billItemList As New List(Of billItemList)()
End Class

2. 修改代码中billItemList的赋值逻辑

不再直接赋值单个对象,而是将对象添加到列表中:

dt1 = dt.ToString("yyyy-MM-dd")
dt2 = dt.AddDays(7).ToString("yyyy-MM-dd")
Dim b As New billItemList With
{
    .name = "*",
    .quantity = 1,
    .unitPrice = txtSubTotal.Value,
    .discount = txtDiscountValue.Value,
    .discountType = "FIXED",
    .vat = 0.15
}

Dim person = New EfaaAPI() With {
    .billNumber = txtInvNo.Value,
    .entityActivityId = 572,
    .customerFullName = CN,
    .customerMobileNumber = txtTelNo.Value,
    .issueDate = dt1,
    .expireDate = dt2
}
' 将单个billItemList对象添加到列表
person.billItemList.Add(b)

Dim json = JsonConvert.SerializeObject(person)

核心逻辑

当属性类型为集合(如List(Of T)、数组)时,Json.NET会自动将其序列化为JSON数组[],而非单个对象,以此满足你的格式需求。

内容的提问来源于stack exchange,提问作者Mohamed Zaher

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 16:00:27