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!
Last edited by Mark@SF; May 6th, 2017 at 01:53 PM.
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.
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:
''' <summary>
''' Allows Serialization of Class Objects to JSON using fastJSON
''' <param name="objectToSerialize">The object to be Serialised</param>
''' <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>
''' <returns>JSON String</returns>
''' <remarks></remarks>
Private Function SerializeToJSON(Of T)(ByVal objectToSerialize As T, ByVal usefastJSONExtensions As Boolean) As String
'To install fastJSON then use the NuGet Package Manager Console with PM> Install-Package fastJSON -Version 2.1.27
'By default this method uses the fastJSON internal extensions
If usefastJSONExtensions Then
Return JSON.ToJSON(objectToSerialize)
Else
Dim jsonParams As New JSONParameters
jsonParams.UseExtensions = False
Return JSON.ToJSON(objectToSerialize, jsonParams)
End If
End Function
''' <summary>
''' Allows Deserialization of JSON String to the Given Class Object Type
''' </summary>
''' <typeparam name="T">The Object Type to be Deserialized to</typeparam>
''' <param name="jsonStringToDeserialize">the JSON String to be Deserialized</param>
''' <returns>An Object of the Required Type</returns>
''' <remarks></remarks>
Private Function DeserializeFromJSON(Of T)(ByVal jsonStringToDeserialize As String) As T
'To install fastJSON then use the NuGet Package Manager Console with PM> Install-Package fastJSON -Version 2.1.27
'Now you can do whatever you want with your class object
MsgBox(deserialisedDataList.Count)
MsgBox(deserialisedDataList(0).dataValue1)
MsgBox(deserialisedDataList(9).dataValue3)
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.