【发布时间】:2018-01-30 16:33:19
【问题描述】:
我正在尝试解析 json,但事情并不完全正常......
我有这个json代码,来自一些股票信息检索api:
{
"Meta Data": {
"1. Information": "Daily Time Series with Splits and Dividend Events",
"2. Symbol": "UNG",
"3. Last Refreshed": "2018-01-29",
"4. Output Size": "Full size",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2018-01-29": {
"1. open": "26.0700",
"2. high": "26.9000",
"3. low": "26.0400",
"4. close": "26.8400",
"5. adjusted close": "26.8400",
"6. volume": "7056837",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
"2018-01-26": {
"1. open": "26.7500",
"2. high": "27.0000",
"3. low": "26.6700",
"4. close": "26.7700",
"5. adjusted close": "26.7700",
"6. volume": "6329877",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
"2018-01-25": {
"1. open": "26.2800",
"2. high": "26.7600",
"3. low": "25.9800",
"4. close": "26.1700",
"5. adjusted close": "26.1700",
"6. volume": "6235136",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
"2018-01-24": {
"1. open": "26.0500",
"2. high": "26.4400",
"3. low": "25.5400",
"4. close": "25.7200",
"5. adjusted close": "25.7200",
"6. volume": "7197720",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
"2018-01-23": {
"1. open": "25.6200",
"2. high": "26.4200",
"3. low": "25.4400",
"4. close": "25.9400",
"5. adjusted close": "25.9400",
"6. volume": "7943240",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
"2018-01-22": {
"1. open": "24.6500",
"2. high": "24.8800",
"3. low": "24.5800",
"4. close": "24.8500",
"5. adjusted close": "24.8500",
"6. volume": "3674144",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
"2018-01-19": {
"1. open": "25.0000",
"2. high": "25.3200",
"3. low": "24.7250",
"4. close": "24.8600",
"5. adjusted close": "24.8600",
"6. volume": "4292913",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
}
我创建了以下类,因为我希望能够将 json 反序列化为以下对象...
Public Class JJSON
Public json As JSON_Container
End Class
Public Class MetaData
<JsonProperty(PropertyName:="1. Information")>
Public Property information
<JsonProperty(PropertyName:="2. Symbol")>
Public Property symbol
<JsonProperty(PropertyName:="3. Last Refreshed")>
Public Property last_refreshed
<JsonProperty(PropertyName:="4. Output Size")>
Public Property output_size
<JsonProperty(PropertyName:="5. Time Zone")>
Public Property time_zone
End Class
Public Class JSON_Container
<JsonProperty(PropertyName:="Meta Data")>
Private Meta As MetaData
<JsonProperty(PropertyName:="Time Series (Daily)")>
Public Time_Series_Daily As StockDate
End Class
Public Class StockDate
Public Dt As List(Of StockInfo)
End Class
Public Class StockInfo
<JsonProperty(PropertyName:="1. open")>
Public Property open As String
<JsonProperty(PropertyName:="2. high")>
Public Property high As String
<JsonProperty(PropertyName:="3. low")>
Public Property low As String
<JsonProperty(PropertyName:="4. close")>
Public Property close As String
<JsonProperty(PropertyName:="5. adjusted close")>
Public Property adjusted_close As String
<JsonProperty(PropertyName:="6. volume")>
Public Property volume As String
<JsonProperty(PropertyName:="7. dividend amount")>
Public Property dividend_amount As String
<JsonProperty(PropertyName:="8. split coefficient")>
Public Property split_coefficient As String
End Class
而且,当我执行这段代码时:
Dim obj1 = JsonConvert.DeserializeObject(Of JSON_Container)(json)
它部分工作,因为只有“元数据”被正确解析。在这一点上我已经尝试了所有我能想到的。
任何帮助将不胜感激..
【问题讨论】:
-
尝试将 TimeSeries 定义为
Dictionary(Of String, StockInfo)按原样,您不会知道它们属于哪个日期/集。此外,发布的 JSON 无效,看起来你缩短了它 -
好的 - 让我试试...
-
Public TimeSeries As Dictionary(Of DateTime, StockInfo)在您的JSON_Container课程中也可以正常工作。另请注意,Parsing与您正在做的不一样Deserializing -
当我按照您的建议进行操作时,它确实有效 - 感谢您的帮助!
-
重复@Plutonix 使用字典的建议:Generating Class VB.NET 和 Parsing Complex JSON with C#。