正在解析来自 ALPHA VANTAGE VB.NET 的带有句点的 JSON 数据

Parsing JSON data with a period from ALPHA VANTAGE VB.NET

我正在尝试从 alpha vantage 解析数据,但对象内部的属性有一段时间导致它 return 什么都没有。

    Public Sub GetPKGStats(symbol As String)
        Dim StatsUrl, result, Open, Close, Volume, LastRefreshed As String
        Dim JSONresult As JObject     
        Dim results As List(Of JToken)

        result = "{""Meta Data"": {""1. Information"": ""Daily Prices (open, high, low, close) and Volumes"",""2. Symbol"": ""PKG"",""3. Last Refreshed"": ""2021-02-16"",""4. Output Size"": ""Compact"",""5. Time Zone"": ""US/Eastern""},""Time Series (Daily)"": {""2021-02-16"": {""1. open"": ""133.4500"",""2. high"": ""133.5650"",""3. low"": ""131.5000"",""4. close"": ""133.0400"",""5. volume"": ""638371""},""2021-02-12"": {""1. open"": ""131.0400"",""2. high"": ""133.9200"",""3. low"": ""131.0400"",""4. close"": ""133.1800"",""5. volume"": ""562984""},""2021-02-11"": {""1. open"": ""133.9900"",""2. high"": ""134.4900"",""3. low"": ""130.3700"",""4. close"": ""131.1700"",""5. volume"": ""768588""}}}"
    JSONresult = JObject.Parse(result)   
    results = JSONresult.Children().ToList()

     For each item As JProperty In results
         LastRefreshed =  item.First.SelectToken("1.Information")
     Next    
End Sub

这不会 return 任何东西,但是如果我从“元数据”对象的第一个 属性 中删除句点,return 就可以了。我正在考虑对句点的 returned 字符串进行替换,但是如果它们中有句点,这可能会影响值。

这是一个缩短的 JSON 字符串示例。

{
"Meta Data": {
    "1. Information": "Daily Prices (open, high, low, close) and Volumes",
    "2. Symbol": "PKG",
    "3. Last Refreshed": "2021-02-16",
    "4. Output Size": "Compact",
    "5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
    "2021-02-16": {
        "1. open": "133.4500",
        "2. high": "133.5650",
        "3. low": "131.5000",
        "4. close": "133.0400",
        "5. volume": "638371"
    },
    "2021-02-12": {
        "1. open": "131.0400",
        "2. high": "133.9200",
        "3. low": "131.0400",
        "4. close": "133.1800",
        "5. volume": "562984"
    },
    "2021-02-11": {
        "1. open": "133.9900",
        "2. high": "134.4900",
        "3. low": "130.3700",
        "4. close": "131.1700",
        "5. volume": "768588"
    },
    "2021-02-10": {
        "1. open": "132.9800",
        "2. high": "134.1200",
        "3. low": "132.5200",
        "4. close": "133.7900",
        "5. volume": "715949"
    }
  }
}

有没有办法仅在接收字符串变量“results”中的字符串时更改 属性 名称

如果您像这样构建 类,使用 JsonProperty 映射属性,它可能会起作用:

Public Class MetaData

    <JsonProperty("1. Information")>
    Public Property Information As String

    <JsonProperty("2. Symbol")>
    Public Property Symbol As String

    <JsonProperty("3. Last Refreshed")>
    Public Property LastRefreshed As String

    <JsonProperty("4. Output Size")>
    Public Property OutputSize As String

    <JsonProperty("5. Time Zone")>
    Public Property TimeZone As String

End Class

Public Class TimeSeriesData
    
    <JsonProperty("1. open")>
    Public Property Open As Decimal

    <JsonProperty("2. high")>
    Public Property High As Decimal

    <JsonProperty("3. low")>
    Public Property Low As Decimal

    <JsonProperty("4. close")>
    Public Property Close As Decimal

    <JsonProperty("5. volume")>
    Public Property Volume As Integer

End Class

使用 类 设置,然后您将使用 DeserializeObject 解析相应的对象:

Dim obj = JObject.Parse(results)
Dim meta = JsonConvert.DeserializeObject(Of MetaData)(obj.First.First.ToString())
Dim dailyTimeSeries = JsonConvert.DeserializeObject(Of Dictionary(Of DateTime, TimeSeriesData))(obj.Last.First.ToString())

演示:https://dotnetfiddle.net/R1WXok