Json序列化器返回空字符串

时间:2019-04-19 08:44:58

标签: json vb.net class json.net

我有一个如下代码的课程。并且我尝试将其转换为XML和JSON,但是它们都返回空值:{}

Public Class PivotSet
        Public Shared Property selectedlines As Boolean()
        Public Shared Property selectedlabels As Boolean()
        Public Shared Property linescolors As Integer()
        Public Shared Property labelcolors As Integer()
        Public Shared Property linethick As Boolean()
        Public Shared Property labelbold As Boolean()
        Public Shared Property pivotPeriod As String

        Public Sub New()
            selectedlines = {True, True, True, True, True, True, True}
            selectedlabels = {True, True, True, True, True, True, True}
            linescolors = {16711680, 255, 255, 255, 32768, 32768, 32768}
            labelcolors = {16711680, 255, 255, 255, 32768, 32768, 32768}
            linethick = {True, True, True, True, True, True, True}
            labelbold = {True, True, True, True, True, True, True}
            pivotPeriod = "Yearly"
        End Sub

和以下代码将其转换为XML:

        Dim settings As New PivotSet
        Dim x As New Xml.Serialization.XmlSerializer(settings.GetType)
        Dim js As New JsonSerializer()
        Dim fs As New FileStream("d:\set.xml", FileMode.Create)
        Dim writer As New StreamWriter(fs, New System.Text.UTF8Encoding)
'serialize to XML
        x.Serialize(writer, settings)
'serialize to json
        File.WriteAllText("d:\Settings.json", JsonConvert.SerializeObject(settings))

但是JSON的结果是{},XML的结果也是

<?xml version="1.0" encoding="utf-8"?>
<PivotSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

有人可以帮助我解决这些问题吗?

1 个答案:

答案 0 :(得分:0)

从属性中删除共享,它应该可以工作:

Public Property selectedlines As Boolean()
Public Property selectedlabels As Boolean()
Public Property linescolors As Integer()
Public Property labelcolors As Integer()
Public Property linethick As Boolean()
Public Property labelbold As Boolean()
Public Property pivotPeriod As String