Results 1 to 4 of 4

Thread: JSON and Yahoo! Finance data

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    487

    JSON and Yahoo! Finance data

    Yahoo! Finance now delivers its data via JSON (in the past it was in HTML tables). I was using the HTML Agility Pack to collect stock, fund, and ETF data from Yahoo! Finance. So now I need to learn how to work with JSON data and would like a bit of help getting started. I see a number of VBForums threads regarding JSON, including discussion of JSON.Net (deserialize, serialize, etc.). The discussions I've seen typically show a code snipped for how to call the deserialize method and sometimes include a class definition (or multiple class definitions) that appear to be used to collect the target data. I haven't found a fully functional code example yet.

    The attached .zip file has a simple demo project that I put together that will get yield (fund or ETF) or dividend (stock) from the Yahoo! Finance website. The demo uses a Regular Expression match to parse the text that is returned from my WebRequest.

    I would appreciate some guidance for how to use JSON.Net to get this data, instead of using a Regular Expression match. Once I am up-to-speed on how to get yield data from the Yahoo! Finance website, then I will be able to use that understanding to get other data as well (e.g., ex-dividend dates).

    Any code examples would be very much appreciated. Thanks in advance for any assistance on this topic!
    Attached Files Attached Files
    Last edited by Mark@SF; May 6th, 2017 at 01:53 PM.

  2. #2
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: JSON and Yahoo! Finance data

    I'm on a similar quest. I did some research and learned that .NET Framework now has a JSON library so one doesn't need to use JSON.NET. You might want to check into that.

  3. #3
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: JSON and Yahoo! Finance data

    Hi,

    I noticed that this had not really been answered yet and I do a lot with JSON structures so I thought this may help. I personally use fastJSON to do my work which can be added to your project using the NuGet package manager using PM> Install-Package fastJSON -Version 2.1.27

    To then use fastJSON here are the Serialization and Deserialization functions that I use which also uses Generics to allow you to send any object type to the function:-

    VB.NET Code:
    1. ''' <summary>
    2. ''' Allows Serialization of Class Objects to JSON using fastJSON
    3. ''' </summary>
    4. ''' <typeparam name="T">Any Object Type</typeparam>
    5. ''' <param name="objectToSerialize">The object to be Serialised</param>
    6. ''' <param name="usefastJSONExtensions">Should be True when Basic Serialisation is required to optimise fastJSON. Should be False when serializing an object for another system or for when not deserializing an object to the same type but that object is of the same structure.</param>
    7. ''' <returns>JSON String</returns>
    8. ''' <remarks></remarks>
    9. Private Function SerializeToJSON(Of T)(ByVal objectToSerialize As T, ByVal usefastJSONExtensions As Boolean) As String
    10.   'To install fastJSON then use the NuGet Package Manager Console with PM> Install-Package fastJSON -Version 2.1.27
    11.   'By default this method uses the fastJSON internal extensions
    12.   If usefastJSONExtensions Then
    13.     Return JSON.ToJSON(objectToSerialize)
    14.   Else
    15.     Dim jsonParams As New JSONParameters
    16.     jsonParams.UseExtensions = False
    17.     Return JSON.ToJSON(objectToSerialize, jsonParams)
    18.   End If
    19. End Function
    20.  
    21. ''' <summary>
    22. ''' Allows Deserialization of JSON String to the Given Class Object Type
    23. ''' </summary>
    24. ''' <typeparam name="T">The Object Type to be Deserialized to</typeparam>
    25. ''' <param name="jsonStringToDeserialize">the JSON String to be Deserialized</param>
    26. ''' <returns>An Object of the Required Type</returns>
    27. ''' <remarks></remarks>
    28. Private Function DeserializeFromJSON(Of T)(ByVal jsonStringToDeserialize As String) As T
    29.   'To install fastJSON then use the NuGet Package Manager Console with PM> Install-Package fastJSON -Version 2.1.27
    30.   Return DirectCast(JSON.ToObject(jsonStringToDeserialize, GetType(T)), T)
    31. End Function

    To then demonstrate using these functions let’s use a class object of:-
    VB.NET Code:
    1. 'IMPORTANT, when deserializing JSON strings to a class object with fastJSON the class must be defined as Public otherwise exceptions are thrown.
    2. Public Class ExampleDataStructure
    3.   Public Property dataValue1 As String
    4.   Public Property dataValue2 As Integer
    5.   Public Property dataValue3 As Double
    6.   Public Property dataValue4 As Boolean
    7. End Class

    With the following example:-
    VB.NET Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.   'Lets create some class level data to demonstrate JSON serialisation
    3.   Dim dataList As New List(Of ExampleDataStructure)
    4.   For loopCount As Integer = 1 To 10
    5.     Dim dataExample As New ExampleDataStructure
    6.     With dataExample
    7.       .dataValue1 = String.Format("Test Data {0}", loopCount)
    8.       .dataValue2 = loopCount * 50
    9.       .dataValue3 = loopCount * 1.23
    10.       .dataValue4 = CBool(loopCount \ 2)
    11.     End With
    12.     dataList.Add(dataExample)
    13.   Next
    14.  
    15.   'Lets serialise the class object to JSON
    16.   Dim jsonString As String = SerializeToJSON(Of List(Of ExampleDataStructure))(dataList, False)
    17.  
    18.   'This can now be saved to file, displayed or sent to your required destination as you need
    19.   MsgBox(jsonString)
    20.  
    21.   'Now we have a JSON string, regardless of where the JSON string came from, we can now deserialise that string to a class object in .NET
    22.   'This is done by creating a class to match the definition of the JSON string. (we will use our example class above with our JSON string)
    23.   Dim deserialisedDataList As List(Of ExampleDataStructure)
    24.     deserialisedDataList = DeserializeFromJSON(Of List(Of ExampleDataStructure))(jsonString)
    25.  
    26.   'Now you can do whatever you want with your class object
    27.   MsgBox(deserialisedDataList.Count)
    28.   MsgBox(deserialisedDataList(0).dataValue1)
    29.   MsgBox(deserialisedDataList(9).dataValue3)
    30. End Sub

    Hopefully this example will speak for itself and help you get to where you need to be but the only other thing to mention may be the “usefastJSONExtensions” parameter. If this is set to True then this allows you to Serialise and Deserialise really complex objects such as TextBox’s etc but it adds a huge amount of additional information to the JSON string thereby bloating the object. Try changing the value in the example and you will see what I mean.

    BTW, dont forget to add the "Imports fastJSON" statement at the start of your code.
    Last edited by IanRyder; Oct 20th, 2017 at 06:05 AM.

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: JSON and Yahoo! Finance data

    There are also several NuGet packages that claim to provide an interface to the Yahoo! Finance APIs, have you tried any of them?
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width