我最近使用vb.net启动了一个项目,我正在努力弄清楚如何获取Mid类中的几个项目的数据,o,h,l,c转换为double并加载到一个数组,最终对它们执行一些数学函数。下面是我希望使用的JSON数据的一小部分(1000个中的3个蜡烛)。
{
"instrument": "EUR_USD",
"granularity": "M1",
"candles": [
{
"complete": true,
"volume": 18,
"time": "2017-07-21T04:13:00.000000000Z",
"mid": {
"o": "1.16281",
"h": "1.16284",
"l": "1.16274",
"c": "1.16281"
}
},
{
"complete": true,
"volume": 96,
"time": "2017-07-21T20:58:00.000000000Z",
"mid": {
"o": "1.16640",
"h": "1.16642",
"l": "1.16628",
"c": "1.16628"
}
},
{
"complete": true,
"volume": 32,
"time": "2017-07-21T20:59:00.000000000Z",
"mid": {
"o": "1.16628",
"h": "1.16652",
"l": "1.16628",
"c": "1.16641"
}
}
]
}
以下是相关代码:
Imports Newtonsoft.Json
Public Class Rootobject
Public Property instrument As String
Public Property granularity As String
Public Property candles() As List(Of Candle)
End Class
Public Class Candle
Public Property complete As Boolean
Public Property volume As Integer
Public Property time As String
Public Property mid As Mid
End Class
Public Class Mid
Public Property o As String
Public Property h As String
Public Property l As String
Public Property c As String
End Class
... 'jsonstring loaded with data here
Dim obj = JsonConvert.DeserializeObject(Of Rootobject)(jsonstring)
我尝试使用循环执行类似于下面的操作,仅接收错误。
Dim obj2 = obj.candles(0).mid.o
我也试图找到使用JObject.Parse(jsonstring)的方法而没有任何成功。那么,具体到什么是将Mid类中的值加载到数组中以进行进一步处理的最佳方法是什么?
提前致谢。
答案 0 :(得分:1)
如果要将所有Mid
个对象放入数组中,可以使用Linq从集合中投影它们。
Dim mids As Mid() = obj.candles.Select(Function(candle) candle.mid).ToArray()
如果您想要特定Mid属性的集合,只需选择您想要的
Dim os As Double() = obj.candles.Select(Function(candle) Double.Parse(candle.mid.o)).ToArray()
或在
之前从mids
数组项目中获取它
Dim os As Double() = mids.Select(Function(mid) Double.Parse(mid.o)).ToArray()
对mid