反序列化'详细' JSON

时间:2018-01-30 16:33:20

标签: json vb.net

我正在尝试解析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)

部分工作,因为只有&#34;元数据&#34;得到正确解析。我已经尝试了我能想到的一切。

任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:0)

我创建了以下用于序列化/反序列化JSON文字的代码:http://www.vbforums.com/showthread.php?858459-Serialize-and-Deserialize-JSON

如果您使用了代码,则使用以下内容:

Dim obj As JSON.Object = JSON.Convert.Deserialize(literal)
Dim meta_data As JSON.Object = obj.Values.Item("Meta Data")
Dim time_series As JSON.Object = obj.Values.Item("Time Series (Daily)")
Dim dates() As JSON.Object = time_series.Values.Select(Function(kvp) DirectCast(kvp.Value, JSON.Object)).ToArray()