Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15917

VS 2019 [RESOLVED] Help with JSON data

$
0
0
I need some advice on how to iterate through a list (deserialized JSON to an object).

Here is the JSON string:

Code:

{"riskStatistics":[{"year":"5y","alpha":{"raw":-3.28,"fmt":"-3.28"},"beta":{"raw":1.2,"fmt":"1.2"},"meanAnnualReturn":{"raw":0.04,"fmt":"0.04"},"rSquared":{"raw":93.34,"fmt":"93.34"},"stdDev":{"raw":18.38,"fmt":"18.38"},"sharpeRatio":{"raw":-0.04,"fmt":"-0.04"},"treynorRatio":{"raw":-2,"fmt":"-2"}},{"year":"3y","alpha":{"raw":-4.46,"fmt":"-4.46"},"beta":{"raw":1.21,"fmt":"1.21"},"meanAnnualReturn":{"raw":-0.16,"fmt":"-0.16"},"rSquared":{"raw":94.64,"fmt":"94.64"},"stdDev":{"raw":19.8,"fmt":"19.8"},"sharpeRatio":{"raw":-0.18,"fmt":"-0.18"},"treynorRatio":{"raw":-4.59,"fmt":"-4.59"}},{"year":"10y","alpha":{"raw":-0.38,"fmt":"-0.38"},"beta":{"raw":1.12,"fmt":"1.12"},"meanAnnualReturn":{"raw":0.52,"fmt":"0.52"},"rSquared":{"raw":93.8,"fmt":"93.8"},"stdDev":{"raw":17.57,"fmt":"17.57"},"sharpeRatio":{"raw":0.32,"fmt":"0.32"},"treynorRatio":{"raw":3.76,"fmt":"3.76"}}]}
Here are the classes that I'm using to deserialize the JSON data:

Code:

Private _riskStatistic As List(Of RiskStatistic)

Private _fmtAlpha As String
Private _rawAlpha As Double
Private _fmtBeta As String
Private _rawBeta As Double
Private _fmtMeanAnnualReturn As String
Private _rawMeanAnnualReturn As Double
Private _fmtRSquared As String
Private _rawRSquared As Double
Private _fmtStdDev As String
Private _rawStdDev As Double
Private _fmtSharpeRatio As String
Private _rawSharpeRatio As Double
Private _fmtTreynorRatio As String
Private _rawTreynorRatio As Double

Private _year As String
Private _alpha As Alpha
Private _beta As Beta
Private _meanAnnualReturn As MeanAnnualReturn
Private _rSquared As RSquared
Private _stdDev As StdDev
Private _sharpeRatio As SharpeRatio
Private _treynorRatio As TreynorRatio

Public Class Alpha

    Public Property raw As Double
        Get
            Return _rawAlpha
        End Get
        Set
            _rawAlpha = Value
        End Set
    End Property

    Public Property fmt As String
        Get
            Return _fmtAlpha
        End Get
        Set
            _fmtAlpha = Value
        End Set
    End Property
End Class
Public Class Beta

    Public Property raw As Double
        Get
            Return _rawBeta
        End Get
        Set
            _rawBeta = Value
        End Set
    End Property

    Public Property fmt As String
        Get
            Return _fmtBeta
        End Get
        Set
            _fmtBeta = Value
        End Set
    End Property
End Class
Public Class MeanAnnualReturn

    Public Property raw As Double
        Get
            Return _rawMeanAnnualReturn
        End Get
        Set
            _rawMeanAnnualReturn = Value
        End Set
    End Property

    Public Property fmt As String
        Get
            Return _fmtMeanAnnualReturn
        End Get
        Set
            _fmtMeanAnnualReturn = Value
        End Set
    End Property
End Class
Public Class RSquared

    Public Property raw As Double
        Get
            Return _rawRSquared
        End Get
        Set
            _rawRSquared = Value
        End Set
    End Property

    Public Property fmt As String
        Get
            Return _fmtRSquared
        End Get
        Set
            _fmtRSquared = Value
        End Set
    End Property
End Class
Public Class StdDev

    Public Property raw As Double
        Get
            Return _rawStdDev
        End Get
        Set
            _rawStdDev = Value
        End Set
    End Property

    Public Property fmt As String
        Get
            Return _fmtStdDev
        End Get
        Set
            _fmtStdDev = Value
        End Set
    End Property
End Class
Public Class SharpeRatio

    Public Property raw As Double
        Get
            Return _rawSharpeRatio
        End Get
        Set
            _rawSharpeRatio = Value
        End Set
    End Property

    Public Property fmt As String
        Get
            Return _fmtSharpeRatio
        End Get
        Set
            _fmtSharpeRatio = Value
        End Set
    End Property
End Class
Public Class TreynorRatio

    Public Property raw As Double
        Get
            Return _rawTreynorRatio
        End Get
        Set
            _rawTreynorRatio = Value
        End Set
    End Property

    Public Property fmt As String
        Get
            Return _fmtTreynorRatio
        End Get
        Set
            _fmtTreynorRatio = Value
        End Set
    End Property
End Class
Public Class RiskStatistic

    Public Property year As String
        Get
            Return _year
        End Get
        Set
            _year = Value
        End Set
    End Property

    Public Property alpha As Alpha
        Get
            Return _alpha
        End Get
        Set
            _alpha = Value
        End Set
    End Property

    Public Property beta As Beta
        Get
            Return _beta
        End Get
        Set
            _beta = Value
        End Set
    End Property

    Public Property meanAnnualReturn As MeanAnnualReturn
        Get
            Return _meanAnnualReturn
        End Get
        Set
            _meanAnnualReturn = Value
        End Set
    End Property

    Public Property rSquared As RSquared
        Get
            Return _rSquared
        End Get
        Set
            _rSquared = Value
        End Set
    End Property

    Public Property stdDev As StdDev
        Get
            Return _stdDev
        End Get
        Set
            _stdDev = Value
        End Set
    End Property

    Public Property sharpeRatio As SharpeRatio
        Get
            Return _sharpeRatio
        End Get
        Set
            _sharpeRatio = Value
        End Set
    End Property

    Public Property treynorRatio As TreynorRatio
        Get
            Return _treynorRatio
        End Get
        Set
            _treynorRatio = Value
        End Set
    End Property
End Class
Public Class Root

    Public Property riskStatistics As List(Of RiskStatistic)
        Get
            Return _riskStatistic
        End Get
        Set
            _riskStatistic = Value
        End Set
    End Property
End Class

Here is the code to deserialize the JSON:

Code:

Dim myDeserializedClass As Root
myDeserializedClass = JsonConvert.DeserializeObject(Of Root)(strRiskStatistics)
If flgDebug Then
    Debug.Print(myDeserializedClass.riskStatistics.Count)
    For i As Integer = 0 To myDeserializedClass.riskStatistics.Count - 1
        Debug.WriteLine("{0}: year = {1}, stdDev = {2}", i, myDeserializedClass.riskStatistics(i).year, myDeserializedClass.riskStatistics(i).stdDev.fmt)
    Next i
End If

In the above code snippet, the first Debug.Print statement shows that there are 3 items in the myDeserializedClass.riskStatistics list (for the "5y", "3y", and "10y" year periods). But when I iterate through the list, the Debug.WriteLine statements all show the same data.

Code:

0: year = 10y, stdDev = 17.57
1: year = 10y, stdDev = 17.57
2: year = 10y, stdDev = 17.57

The expected information should be as follows:

Code:

0: year = 5y, stdDev = 18.38
1: year = 3y, stdDev = 9.8
2: year = 10y, stdDev = 17.57


What am I doing wrong?

Viewing all articles
Browse latest Browse all 15917

Latest Images

Trending Articles



Latest Images