使用DataContractJsonSerializer在VB.NET中将JSON解析为动态类型

时间:2016-01-21 14:05:50

标签: json vb.net object serialization dynamic

我正在使用VB.NET来解析我的SearchSpring JSON:

StrConv(Cells(cell.Row, "B").Value, vbProperCase) = "Due"

问题是JSON对于相同的属性名称有不同的类型("值"):

        Using ms As New MemoryStream(Encoding.Unicode.GetBytes(json))
            Dim serializer As DataContractJsonSerializer = New DataContractJsonSerializer(obj.GetType())
            obj = CType(serializer.ReadObject(ms), T)
            Return obj
        End Using

有时它是字符串(例如 - " Transitional"),有时它是一个对象。

我在序列化中使用这个类。

{
  "field": "vanity_style_63",
  "value": "Transitional",
  "label": "Vanity Style: Transitional",
  "filterLabel": "Vanity Style",
  "filterValue": "Transitional"
},
{
"field": "vanity_width_88",
"value": {
      "rangeLow": "15",
      "rangeHigh": "24"
},
"label": "Vanity Width: 16\" to 24\"",
"filterLabel": "Vanity Width",
"filterValue": "16\" to 24\""
},

当我在那里有弦乐时它会起作用。但是,当JSON在那里有对象时,我无法访问它的值。

我该怎么办?

更新: 这是完全有效的JSON的样子:

<DataContract()> _
Public Class FilterSummary
    <DataMember(Name:="field")> _
    Public Property Field As String
    <DataMember(Name:="filterLabel")> _
    Public Property FilterLabel As String
    <DataMember(Name:="filterValue")> _
    Public Property FilterValue As String
    <DataMember(Name:="label")> _
    Public Property Label As String
    <DataMember(Name:="value")> _
    Public Property Value As Object
End Class

0 个答案:

没有答案