你怎么能Json序列化一个对象?

时间:2017-06-05 21:07:41

标签: vb.net

Private Sub Button29_Click(sender As System.Object, e As System.EventArgs) Handles Button29.Click

    Dim T = New With {Key .auth = "amVyb2VuLmJha2tlckBjb25uZWN0cmljay5ubDpxa2FXUjNaRkx1ZjdxRDAyVXVvd25iOVQ5YWFxaEU9aHpreWxvOjQ2OGZlMTkxNjk2MGFiZmZkNDVlY2VhYTQ5MzA1NzVi"}
    JsonHelper.JsonSerializer(T)

End Sub

Public Class JsonHelper
    Public Shared Function JsonSerializer(Of T)(ByVal obj As T) As String
        Dim ser As New DataContractJsonSerializer(GetType(T))
        Dim ms As New MemoryStream()
        ser.WriteObject(ms, obj)
        Dim jsonString As String = Encoding.UTF8.GetString(ms.ToArray())
        ms.Close()

        Return jsonString

    End Function
End Class

ser.WriteObject(ms, obj),我收到错误:

  

VB $ AnonymousType_0`1 [System.String]类型无法序列化。若要解决此问题,请键入具有DataContractAttribute属性的类型,并使用DataMemberAttribute标记要序列化的所有成员。如果类型是集合,则可以通过使用CollectionDataContractAttribute标记它来绕过此问题。有关其他受支持的类型,请参阅Microsoft .NET文档。

我该如何解决这个问题?

0 个答案:

没有答案