0

我正在尝试序列化 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 对象中?

4

2 回答 2

0

将 datacontractjsonserializersettings 添加到 DataContractDataSerializer 上的构造函数

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializersettings.emittypeinformation.aspx

尝试在构造函数中使用 rootname 设置。

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializersettings.rootname.aspx

于 2013-09-13T01:34:54.673 回答
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
于 2013-09-16T14:06:09.743 回答