vb.net - 从 ALPHA VANTAGE VB.NET 解析带有句点的 JSON 数据
问题描述
我正在尝试从 alpha vantage 解析数据,但对象内部的属性中有一个句点,导致它什么也不返回。
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
这不会返回任何内容,但是如果我从“元数据”对象的第一个属性中删除句点,返回就好了。我正在考虑对返回的字符串进行替换,但如果它们中有句点,这可能会影响值。
这是一个缩短的 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())
推荐阅读
- java - 我做一个要求我创建一个电报机器人机器人的任务
- list - 使用列表理解绘制单独的数字
- flutter - SingleChildScrollView 不可滚动
- html - Safari 5.1.7 浏览器中的 date-input-polyfill
- java - 如何在不使用 indexOf()、contains() 等的情况下编写一个 java 程序来检查一个字符串是否是另一个字符串的子字符串?
- node.js - 发送短信时命令状态 69 错误
- r - 确保 model.matrix 具有相同数量的训练和测试集列
- three.js - 轨道控制和效果器的问题
- jquery - 如何解析多个同名类的单个实例
- python - 使用 Fetch Api 从 JavaScript 访问 Django View 数据