gpt4 book ai didi

javascript - 使用 VB.net 获取 JSON 格式

转载 作者:行者123 更新时间:2023-12-03 06:05:15 24 4
gpt4 key购买 nike

建议我使用 VB.net 获取如下所述的 JSON 格式

var fieldtypes = {

name: { label: 'Name', type: 'text', icon: 'fa-user' },
firstname: { label: 'First name', type: 'text', icon: 'fa-user' },

}

我想在以 AJAX 调用形式调用的 Web 方法中获取此格式。我为此编写了 VB.net 方法,但它不会生成上面描述的 JSON。

VB.Net 网络方法

Public Class FormBuilder
Public Property label() As String
Public Property type() As String
Public Property icon() As String
End Class

网络方法:

Public Shared Function LogsheetDetail(LogMasterID As Integer) As String
Dim sCtrlTag As String = ""
Dim sDataType As String = ""
Dim finalVal As String = ""
Dim oDs As DataSet
Dim frmBuilder As New List(Of FormBuilder)()
Try
oDs = GenUser.TempLogsheetDetails(Conn, LogMasterID)
If oDs.Tables(0).Rows.Count > 0 Then
For i = 0 To oDs.Tables(0).Rows.Count - 1
sDataType = oDs.Tables(0).Rows(i)("data_type").ToString()
Select Case sDataType
Case "Text"
frmBuilder.Add(New FormBuilder() With { _
.label = oDs.Tables(0).Rows(i)("parameter_name").ToString(), _
.type = "text", _
.icon = "fa-user" _
})


End Select
Next

End If


oDs.Dispose()
Catch ex As Exception
Throw New Exception(ex.ToString)
Finally
If Not oDs Is Nothing Then oDs.Dispose()
End Try
Dim objJSSerializer As New System.Web.Script.Serialization.JavaScriptSerializer()

Dim jsonString As String = objJSSerializer.Serialize(frmBuilder)

Return jsonString

最佳答案

我认为问题在于您的代码创建了一个对象数组而不是单个对象。我假设您的代码生成如下输出:

[
{ "label": "Name", "type": "text", "icon": "fa-user" },
{ "label": "First name", "type": "text", "icon": "fa-user" }
]

理想的解决方案是创建字典而不是列表。但是,JsonSerializer 类不能很好地与字典配合使用 would need a workaround .

如果你可以使用 Newtonsoft Json 而不是 JavascriptSerializer,那就更简单了。以下代码使用 Newtonsoft Json NuGet 包生成您想要的输出:

Dim dic = New Dictionary(Of String, FormBuilder)

dic.Add("name", New FormBuilder() With {
.label = "name",
.type = "text",
.icon = "fa-user"
})

dic.Add("firstname", New FormBuilder() With {
.label = "firstname",
.type = "text",
.icon = "fa-user"
})

Return Newtonsoft.Json.JsonConvert.SerializeObject(dic)

关于javascript - 使用 VB.net 获取 JSON 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39577972/

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