2

这是预期的输出示例(不幸的是,我无法控制):

{
    "the_date":"2013-10-19,"
    "users":
        {
            "john doe":
                {
                    "telephone":"123-456-7890",
                    "email":"jodoe@server.com"
                }
        },
        {
            "jane doe":
                {
                    "telephone":"123-456-7891",
                    "email":"jadoe@server.com"
                }
        }
}

我正在使用对象列表在 vb.net 中创建我的数据,并像这样对其进行序列化:

Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Web.Script.Serialization



Public Class the_data

    Property the_date As String
    Property users As New List(Of KeyValuePair(Of String, List(Of jsuser_data)))

    Public Sub New()

        the_date = "2013-10-19"

        Dim dtUserNames As New DataTable 'hardcoded list for testing only
        dtUserNames.Columns.Add("fldUserId", GetType(Integer))
        dtUserNames.Columns.Add("fldUserName", GetType(String))

        dtUserNames.Rows.Add(100, "john doe")
        dtUserNames.Rows.Add(101, "jane doe")

        Using reader As New DataTableReader(New DataTable() {dtUserNames})
            If reader.HasRows Then
                While (reader.Read())
                    Dim test As New List(Of jsuser_data)
                    test.Add(New jsuser_data(reader("fldUserId")))
                    users.Add(New KeyValuePair(Of String, List(Of jsuser_data))(reader("fldUserName").ToString, test)) 'THIS IS THE LINE OF NOTE
                End While
            End If
        End Using
    End Sub


End Class


Public Class jsuser_data
    Property telephone As String
    Property email As String

    Public Sub New(theUserId As String)

        Dim dtUserData As New DataTable 'hardcoded list for testing only, yes I know it will return the same data for both, I am just simulating my sproc call
        dtUserData.Columns.Add("fldUserId", GetType(Integer))
        dtUserData.Columns.Add("fldTelephone", GetType(String))
        dtUserData.Columns.Add("fldEmail", GetType(String))

        dtUserData.Rows.Add(100, "123-456-7890", "jdoe@server.com")

        Using reader As New DataTableReader(New DataTable() {dtUserData})
            If reader.HasRows Then
                While (reader.Read())
                    telephone = reader("fldTelephone")
                    email = reader("fldEmail")
                End While
            End If
        End Using
    End Sub

End Class

Partial Class Default3
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim jsonString As String
        Dim test As New the_data()

        jsonString = New JavaScriptSerializer().Serialize(test)
        Response.Write(jsonString)
    End Sub
End Class

在尝试了很多不同的方法之后,这和我所得到的一样接近。但是,它正在输出单词“key”和“value”,如下所示:

{
    "the_date":"2013-10-19",
    "users":[
        {"Key":"john doe",
        "Value":[
            {"telephone":"123-456-7890","email":"jdoe@server.com"}
            ]
        },
        {"Key":"jane doe",
        "Value":[
            {"telephone":"123-456-7890","email":"jdoe@server.com"}
            ]
        }
        ]
}

我需要编写自定义 JSON 序列化程序还是缺少什​​么?我试图尽可能彻底地回答我的问题,并且在我所有的尝试和搜索中我都把自己完全弄糊涂了,所以如果需要更多数据来回答,请告诉我。谢谢!

编辑:根据 nicolas-straub-valdivieso 的建议,我已将“the_data”对象更改为:

Public Class the_data

    Property the_date As String
    'Property users As New List(Of KeyValuePair(Of String, List(Of jsuser_data)))
    Property users As New Dictionary(Of String, List(Of jsuser_data))

    Public Sub New()

        the_date = "2013-10-19"

        Dim dtUserNames As New DataTable 'hardcoded list for testing only
        dtUserNames.Columns.Add("fldUserId", GetType(Integer))
        dtUserNames.Columns.Add("fldUserName", GetType(String))

        dtUserNames.Rows.Add(100, "john doe")
        dtUserNames.Rows.Add(101, "jane doe")

        Using reader As New DataTableReader(New DataTable() {dtUserNames})
            If reader.HasRows Then
                While (reader.Read())
                    Dim test As New List(Of jsuser_data)
                    test.Add(New jsuser_data(reader("fldUserId")))
                    users.Add(reader("fldUserName").ToString, test) 'THIS IS THE LINE OF NOTE
                End While
            End If
        End Using
    End Sub


End Class

现在我的输出是:

{
    "the_date":"2013-10-19",
    "users":
        {
            "john doe":[
                {"telephone":"123-456-7890",
                "email":"jdoe@server.com"}
            ],
            "jane doe":[
                {"telephone":"123-456-7890",
                "email":"jdoe@server.com"}
            ]
        }
}

这与我给出的示例输出之间的唯一区别是用户数据周围的 [],这可能没问题。

4

1 回答 1

0

将 KeyValuePair 更改为字典(添加答案,以便您可以将问题标记为已解决)。方括号是必需的,因此 JSON 可以将构造识别为数组(实际上,您被要求的格式会引发解析错误,因为您必须用 {} (对象)或 [ ] (大批)。

编辑:我没有看到您更新的代码...如果您将道具从更改Dictionary(of string,List(of jsuser_data))Dictionary(of string,jsuser_data)额外的大括号将消失。

于 2013-10-19T21:53:04.873 回答