Announcement

Collapse
No announcement yet.

JSON / JavaScriptSerializer HELP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    JSON / JavaScriptSerializer HELP

    I'm stuck and need help using JavaScriptSerializer() with JSON.
    Here is my JSON file:
    Code:
    {
    "Time Series (Daily)": {
    "2020-12-03": {
    "1. open": "123.5200", "2. high": "123.7800", "3. low": "122.2100", "4. close": "122.9400", "5. volume": "78601338"
    },
    "2020-12-02": {
    "1. open": "122.0200", "2. high": "123.3700", "3. low": "120.8900", "4. close": "123.0800", "5. volume": "89004195"
    }, "2020-12-01": {
    "1. open": "121.0100", "2. high": "123.4693", "3. low": "120.0100", "4. close": "122.7200", "5. volume": "125920963"
    }, "2020-11-30": {
    "1. open": "116.9700", "2. high": "120.9700", "3. low": "116.8100", "4. close": "119.0500", "5. volume": "169410176"
    }
    }
    }
    Here is a quick snippet of my VB.net code:
    Code:
    getData = hs.GetURL("https://www.alphavantage.co", "/query?function=TIME_SERIES_DAILY&apikey=DEMO&symbol=GOOGL", False, 80)
    jsonData = New JavaScriptSerializer().Deserialize(Of Object)(getData)
    
    closeCurrent = jsonData("Time Series (Daily)")("2020-12-03")("4. close")
    closePrevious = jsonData("Time Series (Daily)")("2020-12-02")("4. close")
    My issue:
    So as long as I know the object name, in this instance the date, I can pull the data. But how can I just grab the first 2 objects without knowing the date?

    For example something like this (obviously this doesn't work):
    Code:
    closeCurrent = jsonData("Time Series (Daily)")(0)("4. close")
    closePrevious = jsonData("Time Series (Daily)")(1)("4. close")
    Thank you in advance for any help. <3

    #2
    Is there an easier way to accomplish this?

    Comment


      #3
      Why don't you just calculate the date object name i.e.

      For today:

      Dim MyDate As String = datetime.Now.ToString("yyyy-MM-dd")

      For tomorrow:

      Dim MyDate As String = datetime.Now.Adddays(1).ToString("yyyy-MM-dd")

      For yesterday:

      Dim MyDate As String = datetime.Now.Adddays(-1).ToString("yyyy-MM-dd")
      Jon

      Comment


        #4
        Originally posted by jon00 View Post
        Why don't you just calculate the date object name i.e.
        I thought exactly that at first but the stock market is closed on weekends, which of course I could check if the date was a Saturday or Sunday, but then there are the odd days when a holiday is on a weekday and the markets are closed.

        Was hoping maybe there was a way to iterate through the jsonData("Time Series (Daily)") object somehow and grab the key value?

        Thanks again in advance.

        Comment


          #5
          I can't think of a method apart from using Regex on the JSON data first.

          Jon

          Comment


            #6
            Have you thought about converting youor jsonstring into a JObject?
            From there you can search the tokens whare are like the keys you describe.

            In my example below I searching a JSON array for a particular value based on a key. You can pick the level you search if you know the exect structure.
            Show further down is the JSON array I a searching (just to give you an idea of what I am searching through)

            I use Newtonsoft JSON library.
            Hope this helps!

            Other option is to query your json using linq! ( https://www.newtonsoft.com/json/help...LINQtoJSON.htm ; Getting values by Property Name or Collection Index)


            Code:
            Dim o as JObject = JObject.Parse(JsonConvert.SerializeObject(jsonstring))
            Dim observation As JToken = o.SelectToken("$.properties.timeseries[?(@.time == '" & currenttime & "')]")

            This is the JSON I am searching:

            Code:
            {
            "type": "Feature",
            "geometry": {
            "type": "Point"
            },
            "properties": {
            "meta": {
            "updated_at": "2020-11-27T21:23:18Z",
            "units": {
            "air_pressure_at_sea_level": "hPa",
            "air_temperature": "celsius",
            "air_temperature_max": "celsius",
            "air_temperature_min": "celsius",
            "cloud_area_fraction": "%",
            "cloud_area_fraction_high": "%",
            "cloud_area_fraction_low": "%",
            "cloud_area_fraction_medium": "%",
            "dew_point_temperature": "celsius",
            "fog_area_fraction": "%",
            "precipitation_amount": "mm",
            "relative_humidity": "%",
            "ultraviolet_index_clear_sky": "1",
            "wind_from_direction": "degrees",
            "wind_speed": "m/s"
            }
            },
            "timeseries": [
            {
            "time": "2020-11-27T21:00:00Z",
            "data": {
            "instant": {
            "details": {
            "air_pressure_at_sea_level": 1015.2,
            "air_temperature": 13.8,
            "cloud_area_fraction": 46.9,
            "cloud_area_fraction_high": 0.0,
            "cloud_area_fraction_low": 31.2,
            "cloud_area_fraction_medium": 42.2,
            "dew_point_temperature": 7.6,
            "fog_area_fraction": 0.0,
            "relative_humidity": 66.2,
            "ultraviolet_index_clear_sky": 0.0,
            "wind_from_direction": 238.9,
            "wind_speed": 3.9
            }
            },
            "next_12_hours": {
            "summary": {
            "symbol_code": "fair_night"
            }
            },
            "next_1_hours": {
            "summary": {
            "symbol_code": "fair_day"
            },
            "details": {
            "precipitation_amount": 0.0
            }
            },
            "next_6_hours": {
            "summary": {
            "symbol_code": "fair_night"
            },
            "details": {
            "air_temperature_max": 12.4,
            "air_temperature_min": 8.4,
            "precipitation_amount": 0.0
            }
            }
            }
            },
            {
            "time": "2020-11-27T22:00:00Z",
            "data": {
            "instant": {
            "details": {
            "air_pressure_at_sea_level": 1015.2,
            "air_temperature": 12.4,
            "cloud_area_fraction": 31.2,
            "cloud_area_fraction_high": 0.0,
            "cloud_area_fraction_low": 29.7,
            "cloud_area_fraction_medium": 14.1,
            "dew_point_temperature": 6.7,
            "fog_area_fraction": 0.0,
            "relative_humidity": 68.5,
            "ultraviolet_index_clear_sky": 0.0,
            "wind_from_direction": 254.5,
            "wind_speed": 3.9
            }
            },
            "next_12_hours": {
            "summary": {
            "symbol_code": "fair_night"
            }
            },
            "next_1_hours": {
            "summary": {
            "symbol_code": "partlycloudy_night"
            },
            "details": {
            "precipitation_amount": 0.0
            }
            },
            "next_6_hours": {
            "summary": {
            "symbol_code": "fair_night"
            },
            "details": {
            "air_temperature_max": 11.3,
            "air_temperature_min": 7.7,
            "precipitation_amount": 0.0
            }
            }
            }
            },

            Comment

            Working...
            X