在VB.NET中转换JSON数据

时间:2017-07-07 03:51:58

标签: json vb.net

我正在尝试将This Data转换为结构。

我的结构如下:

Public Structure cChartData
    Public cUDate As String
    Public cOpen As Double
    Public cClose As Double
    Public cHigh As Double
    Public cLow As Double
End Structure

和类如此:

Friend Class ChartData
Public Property uDate() As String
    Get
        Return m_date
    End Get
    Set
        m_date = Value
    End Set
End Property
Private m_date As String
Public Property high() As String
    Get
        Return m_high
    End Get
    Set
        m_high = Value
    End Set
End Property
Private m_high As String
Public Property low() As String
    Get
        Return m_low
    End Get
    Set
        m_low = Value
    End Set
End Property
Private m_low As String
Public Property open() As String
    Get
        Return m_open
    End Get
    Set
        m_open = Value
    End Set
End Property
Private m_open As String
Public Property close() As String
    Get
        Return m_close
    End Get
    Set
        m_close = Value
    End Set
End Property
Private m_close As String
Public Property volume() As String
    Get
        Return m_volume
    End Get
    Set
        m_volume = Value
    End Set
End Property
Private m_volume As String
Public Property quoteVolume() As String
    Get
        Return m_quoteVolume
    End Get
    Set
        m_quoteVolume = Value
    End Set
End Property
Private m_quoteVolume As String
Public Property weightedAverage() As String
    Get
        Return m_weightedAverage
    End Get
    Set
        m_weightedAverage = Value
    End Set
End Property
Private m_weightedAverage As String
End Class

我正在尝试从每一行检索所有变量。除了约会,我得到了所有这些。我使用以下代码(其中chartInfo = JSON数据):

Dim cdata = JsonConvert.DeserializeObject(Of List(Of ChartData))(chartInfo)
        Dim cResData(cdata.Count - 1) As cChartData
        For i = 0 To cdata.Count - 1
            cResData(i).cUDate = cdata(i).uDate
            cResData(i).cOpen = Convert.ToDouble(cdata(i).open)
            cResData(i).cClose = Convert.ToDouble(cdata(i).close)
            cResData(i).cHigh = Convert.ToDouble(cdata(i).high)
            cResData(i).cLow = Convert.ToDouble(cdata(i).low)
        Next
        Return cResData

日期返回"空白"显示时显示值,或者没有任何值,但所有其他值都正确返回。这是第一个值,所以我想知道它是否与它有关...

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

当一个属性可以被序列化时,它将被JSON序列化器保留为空。

因此,您的JSON中的日期为空或null,或者它们位于.NET默认情况下无法序列化的DateFormat中。

在您发布的JSON中,日期字段是数字格式,例如'1438992000',表示它是时间戳。

此外,字段名称为“日期”,但您已将属性定义为uDate。要使序列化生效,除非您覆盖默认序列化,否则属性名称必须与JSON键匹配。更改定义如下,它将序列化:

    Public Property [date] As String
    Get
        Return m_date
    End Get
    Set
        m_date = Value
    End Set
End Property

答案 1 :(得分:0)

你得到"空的原因" uDate属性的值是porperty名称应该匹配以便成功反序列化。

您可以更改属性名称以满足此规则。但是如果你想在类中保存属性名称 - 你可以使用JsonPropertyAttribute作为json名称。

Public Class ChartData

    <JsonProperty("date")>
    Public Property uDate As String

    <JsonProperty("high")>
    Public Property high As String

End Class

使用属性,您可以为不同的代码环境使用不同的命名约定。例如&#34; PascalCase&#34;在.NET和&#34; camelCase&#34;在JSON中