Reputation: 39
I have recently started a project using vb.net and I'm struggling to figure out how get the data for several of the items in the Mid class, o,h,l,c converted to double and loaded into an array, to eventually perform some math functions on them. Below is a small (3 candles out of 1000) sample of the JSON data I wish to work with.
{
"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"
}
}
]
}
Here is the relevant code:
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)
I have attempted to do something similar to below with a loop only to receive errors.
Dim obj2 = obj.candles(0).mid.o
I have also tried to find ways of using JObject.Parse(jsonstring) without any success. So, specifically what is the best way to get the values in the Mid class each loaded into an array for further processing?
Thanks in advance.
Upvotes: 2
Views: 1191
Reputation: 247153
If you want to get all the Mid
objects into an array, you can use Linq to project them from the collection.
Dim mids As Mid() = obj.candles.Select(Function(candle) candle.mid).ToArray()
If you want a collection of a specific Mid property just select the one you want
Dim os As Double() = obj.candles.Select(Function(candle) Double.Parse(candle.mid.o)).ToArray()
or grab it from the mids
array project before
Dim os As Double() = mids.Select(Function(mid) Double.Parse(mid.o)).ToArray()
The same can be done for any of the other properties with mid
Upvotes: 1