2017-07-23 75 views
2

我最近開始使用vb.net的一個項目,我正在努力弄清楚如何獲取Mid類中的幾個項目的數據o,h,l, c轉換爲double並加載到數組中,最終對它們執行一些數學函數。以下是我希望使用的JSON數據的一小部分(1000箇中的3個)樣本。在VB.NET中反序列化嵌套的JSON進行轉換

{ 
    "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類中的值加載到數組中以便進一步處理的最佳方法是什麼?

在此先感謝。

回答

1

如果要將所有Mid對象放入數組中,可以使用Linq從集合中進行投影。

Dim mids As Mid() = obj.candles.Select(Function(candle) candle.mid).ToArray() 

如果你想有一個具體的中期性質的集合只需選擇您想要

Dim os As Double() = obj.candles.Select(Function(candle) Double.Parse(candle.mid.o)).ToArray() 

一個或前

Dim os As Double() = mids.Select(Function(mid) Double.Parse(mid.o)).ToArray() 

mids陣列項目抓住它同樣可以用mid

完成任何其他屬性