2017-07-07 105 views
0

我正在嘗試將This Data轉換爲結構。在VB.NET中轉換JSON數據

我有結構爲這樣:

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 

的日期返回一個「空白」的值,或什麼都沒有,顯示時,但所有其他值返回正常。這是第一個值,所以我想知道它是否與它有關...

任何幫助,非常感謝。

回答

0

當一個屬性可以被序列化時,它將被JSON序列化器留空。

因此,無論您的JSON中的日期是空的還是空的,或者它們都是.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 
+0

我已經發布了正在用作主鏈中超鏈接的JSON數據.... –

+0

對不起,沒有看到它。更新了我的答案。 –

+0

問題被命名爲「日期」,但使用[日期]完美工作。非常感謝! –

0

爲什麼你的財產uDate「空」值的原因是,porperty名稱應該匹配成功的反序列化。

您可以更改屬性名稱以滿足此規則。但是,如果您想保留類中的屬性名稱 - 您可以使用JsonPropertyAttribute來獲取json名稱。

Public Class ChartData 

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

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

End Class 

使用屬性可以對不同的代碼環境使用不同的命名約定。例如.NET中的「PascalCase」和JSON中的「camelCase」