0

I am trying to parse json and things are not working quite correctly...

I have this json code, which from some stock information retrieval 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"
    },
}

I created the following classes because I wanted to be able to deserialize the json into the following objects...

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

And, when I execute this code:

Dim obj1 = JsonConvert.DeserializeObject(Of JSON_Container)(json)

It works partially since only the "Meta Data" gets parsed correctly. I have tried everything I could think of at this point.

Any help would be appreciated..

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
PiE
  • 335
  • 1
  • 7
  • 24
  • 2
    Try defining TimeSeries as a `Dictionary(Of String, StockInfo)` As is, you wont know which date/set they belong to. Also, the JSON posted is not valid, looks like you shortened it – Ňɏssa Pøngjǣrdenlarp Jan 30 '18 at 16:40
  • Ok - let me try that... – PiE Jan 30 '18 at 16:41
  • 2
    `Public TimeSeries As Dictionary(Of DateTime, StockInfo)` in your `JSON_Container` class will also work fine. Also note that `Parsing` is not the same and what you are doing which is `Deserializing` – Ňɏssa Pøngjǣrdenlarp Jan 30 '18 at 16:46
  • It does work when I do what you recommend - Thanks for your help! – PiE Jan 30 '18 at 16:55
  • Duplicates for @Plutonix's recommendation to use a dictionary: [Generating Class VB.NET](https://stackoverflow.com/q/45789578/3744182) and [Parsing Complex JSON with C#](https://stackoverflow.com/q/44863460/3744182). – dbc Feb 03 '18 at 00:18

1 Answers1

0

I created the following code that serializes/deserializes JSON literals: http://www.vbforums.com/showthread.php?858459-Serialize-and-Deserialize-JSON

If you used the code, then you use the following:

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()
David
  • 5,877
  • 3
  • 23
  • 40