我正在尝试将这些数据转换为一个结构。
我有这样的结构:
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该日期在显示时返回“空白”值,或不返回任何值,但所有其他值都会正确返回。它是第一个价值,所以我想知道它是否与它有关.
任何帮助都是非常感谢的。
发布于 2017-07-07 03:55:36
当属性可以序列化时,JSON序列化程序将使其为空。
因此,您的JSON中的日期要么是空的,要么是空的,或者是在DateFormat中,.NET默认不能序列化。
在您发布的JSON中,date字段是数字格式,例如“1438992000”,意思是时间戳。
此外,字段名是'date‘,但您已经将属性定义为uDate。要使序列化工作,属性名称必须与JSON键匹配,除非您要重写默认序列化。更改定义如下,它将序列化:
Public Property [date] As String
Get
Return m_date
End Get
Set
m_date = Value
End Set
End Property发布于 2017-07-07 04:12:05
获得uDate属性的“空”值的原因是porperty名称应该与成功的反序列化匹配。
您可以更改属性名以满足此规则。但是,如果希望将属性名称保持在类中,则可以使用JsonPropertyAttribute作为json名称。
Public Class ChartData
<JsonProperty("date")>
Public Property uDate As String
<JsonProperty("high")>
Public Property high As String
End Class有了属性,您可以对不同的代码环境使用不同的命名约定。例如,.NET中的“.NET”和JSON中的"camelCase“
https://stackoverflow.com/questions/44962268
复制相似问题