要序列化为json的对象列表,如何在运行时定义键名?

时间:2013-10-19 15:36:44

标签: asp.net json vb.net list serialization

以下是预期的输出示例(遗憾的是我无法控制):

{
    "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"}
            ]
        }
}

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

1 个答案:

答案 0 :(得分:0)

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

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

相关问题