Json序列化了类名吗?

时间:2016-03-14 09:03:30

标签: json vb.net

大家好,我必须在json中列出一个列表,如下所示

{  
   "ListOfStructures":[{  
      "structure":{  
         "One": "0",
         "Two": "two0"
      },
      "structure":{  
         "One": "0",
         "Two": "two0"
      }
   }]
}

在这一刻,我获得了这个结果:

{  
   "ListOfStructures":[ 
      {  
         "One": "0",
         "Two": "two0"
      },
      {  
         "One": "0",
         "Two": "two0"
      }
   ]
}

如您所见,列表项没有名称,但我需要它......

在vb.net对象之后我必须序列化,并且(可能)必须修改以获得所需的结果:

    Public Class MyObj

        Private _listOfStructures As List(Of Structure)
        Public Property ListOfStructures() As List(Of Structure)
            Get
                Return _listOfStructures
            End Get
            Set(ByVal value As List(Of Structure))
                _listOfStructures= value
            End Set
        End Property

    End Class

    Public Class Structure
        Private _one As String
        Public Property One() As String
            Get
                Return _one
            End Get
            Set(ByVal value As String)
                _one = value
            End Set
        End Property

        Private _two As String
        Public Property Two() As String
            Get
                Return _two
            End Get
            Set(ByVal value As String)
                _two = value
            End Set
        End Property
    End Class

如何获取列表中对象的名称?

谢谢

1 个答案:

答案 0 :(得分:0)

你问的是无效的Json:对象中的每个属性都应该有一个唯一的名称。在您的示例中,您有2个结构属性。

我建议为每个对象添加一个额外的属性,返回类型的名称:

public string typeName { get { return this.GetType().Name; } }