DataContractJsonSerializer在JSON对象中包含对象名称

时间:2013-09-12 22:39:10

标签: json vb.net

我正在尝试序列化JSON对象,以便将其发送到Web服务。我正在使用这个asp.net vb代码:

Dim ser As DataContractJsonSerializer = New DataContractJsonSerializer(postData.GetType())
Dim MS As MemoryStream = New MemoryStream()
ser.WriteObject(MS, postData)
json = Encoding.UTF8.GetString(MS.ToArray())
产生这个JSON的

{"guid":"10049cf5-a622-4aa6-a1d5-58022c4e2a19"}

但我需要的是这个JSON:

{"registerRequest": {"guid":"10049cf5-a622-4aa6-a1d5-58022c4e2a19"}}

这是我要发送的对象的类定义:

Partial Public Class registerRequest
    Private Property _guid As String
    Public Property guid As String
        Get
            Return _guid
        End Get
        Set(value As String)
            _guid = value
        End Set
    End Property

    Sub New()
    End Sub

End Class

如何获取要在JSON对象中显示的类的名称,如上所述?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

好的,要匹配格式我刚刚创建了一个包装类,其中包含一个“registerRequest”对象。一种解决方法,但它有效!

Partial Public Class SerializedRegisterRequest
    Private Property _registerRequest As registerRequest
    Public Property registerRequest As registerRequest
        Get
            Return _registerRequest
        End Get
        Set(value As registerRequest)
            _registerRequest = value
        End Set
    End Property

    Sub New()
    End Sub

End Class

Partial Public Class registerRequest
    Private Property _guid As String
    Public Property guid As String
        Get
            Return _guid
        End Get
        Set(value As String)
            _guid = value
        End Set
    End Property

    Sub New()
    End Sub

End Class