.net 反序列化Google财务JSON字符串
我试图从3家公司获取最新的股票价格,但我似乎无法正确获得这些数字。我可以从google finance中获取符号,我得到一个类似的对象:.net 反序列化Google财务JSON字符串,.net,vb.net,json.net,.net,Vb.net,Json.net,我试图从3家公司获取最新的股票价格,但我似乎无法正确获得这些数字。我可以从google finance中获取符号,我得到一个类似的对象: // [ { "id": "983582" ,"t" : ".DJI" ,"e" : "INDEXDJX" ,"l" : "18,051.10" ,"l_fix" : "18051.10" ,"l_cur" : "18,051.10" ,"s": "0" ,"ltt":"3:15PM EDT" ,"lt" : "Sep 13, 3:15PM EDT" ,"l
// [ { "id": "983582" ,"t" : ".DJI" ,"e" : "INDEXDJX" ,"l" : "18,051.10" ,"l_fix" : "18051.10" ,"l_cur" : "18,051.10" ,"s": "0" ,"ltt":"3:15PM EDT" ,"lt" : "Sep 13, 3:15PM EDT" ,"lt_dts" : "2016-09-13T15:15:39Z" ,"c" : "-273.97" ,"c_fix" : "-273.97" ,"cp" : "-1.50" ,"cp_fix" : "-1.50" ,"ccol" : "chr" ,"pcls_fix" : "18325.07" } ]
我试图获取第四个冒号后面的项目,即“18051.10”,并将其余的项目置零,然后将该双精度值放入数组中,这样我就可以返回该值并显示到标签上,但运气不好。我是一个noob程序员,试图实践一个示例程序,该程序返回一家公司的股票图表历史记录,但我只想返回最新的报价
Imports System.Net
Imports System.IO
Imports System.Drawing.Drawing2D
Public Class Form1
' The ticker symbols.
Private Symbols As List(Of String) = Nothing
' The current prices.
Private Prices() As List(Of Single) = Nothing
' Redraw the graph.
'Private Sub picGraph_Resize(ByVal sender As Object, ByVal e As System.EventArgs)
' DrawGraph()
'End Sub
' Get the closing prices and graph them.
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
Me.Cursor = Cursors.WaitCursor
' Get the ticker symbols.
'Dim symbols_text() As String = txtSymbols.Text.Split(","c)
Dim symbols_text() As String = {"PKG", "TEN", "INDU"}
Symbols = New List(Of String)()
For i As Integer = 0 To symbols_text.Length - 1
Symbols.Add(symbols_text(i).Trim())
Next i
' Get the data.
ReDim Prices(0 To Symbols.Count - 1)
For i As Integer = 0 To Symbols.Count - 1
Prices(i) = GetStockPrices(Symbols(i))
Next i
' Graph it.
'DrawGraph()
FillData()
Me.Cursor = Cursors.Default
End Sub
' Get the prices for this symbol.
'Private Function GetStockPrices(ByVal symbol As String) As List(Of Single)
' ' Compose the URL.
' Dim url As String = "http://www.google.com/finance/historical?output=csv&q=" & symbol
' ' Get the result.
' ' Get the web response.
' Dim result As String = GetWebResponse(url)
' ' Get the historical prices.
' Dim lines() As String = result.Split( _
' New String() {vbCr, vbLf}, _
' StringSplitOptions.RemoveEmptyEntries)
' Dim prices As New List(Of Single)()
' ' Process the lines, skipping the header.
' For i As Integer = 1 To lines.Length - 1
' Dim line As String = lines(i)
' prices.Add(Single.Parse(line.Split(","c)(4)))
' Next i
' Return prices
'End Function
Public Function GetStockPrices(symbol As String) As List(Of Single)
' Dim pricesArray() As String
Dim prices As New List(Of Single)()
Dim items() As GoogleFinanceItem
If UCase(symbol) = "INDU" Then
Dim url As String = "http://finance.google.com/finance/info?client=ig&q=INDEXDJX%3A.DJI"
Dim result As String = GetWebResponse(url)
' Dim newResult = result.Substring(1, result.Length - 1)
items = JsonConvert.DeserializeObject(Of GoogleFinanceItem())(result)
' pricesArray = Split(result, ":")
Else
Dim url As String = "http://finance.google.com/finance/info?client=ig&q=" & UCase(symbol)
Dim result As String = GetWebResponse(url)
' Dim newResult = result.Substring(1, result.Length - 1)
items = JsonConvert.DeserializeObject(Of GoogleFinanceItem())(result)
' pricesArray = Split(result, ":")
End If
' pricesArray = Split(pricesArray(4), """")
' prices.Add(CSng(Format(pricesArray(1), "0.00")))
'prices.Add(CSng(Format(pricesArray(1))))
prices.Add(CSng(items(0).price))
Return prices
End Function
' Get a web response.
Private Function GetWebResponse(ByVal url As String) As String
' Make a WebClient.
Dim web_client As New WebClient()
' Get the indicated URL.
Dim response As Stream = web_client.OpenRead(url)
' Read the result.
Using stream_reader As New StreamReader(response)
' Get the results.
Dim result As String = stream_reader.ReadToEnd()
' Close the stream reader and its underlying stream.
stream_reader.Close()
' Return the result.
Return result
End Using
End Function
Private Sub FillData()
Dim symbolSubSet() As Control = {Label1, Label2, Label3}
Dim priceSubSet() As Control = {Label4, Label5, Label6}
For i = 0 To 2
symbolSubSet(i).Text = Symbols(i)
priceSubSet(i).Text = Prices(i).ToString
Next
End Sub
' Draw the graph.
'Private Sub DrawGraph()
' If (Prices Is Nothing) Then Return
' ' Make the bitmap.
' Dim bm As New Bitmap( _
' picGraph.ClientSize.Width, _
' picGraph.ClientSize.Height)
' Using gr As Graphics = Graphics.FromImage(bm)
' gr.Clear(Color.White)
' gr.SmoothingMode = SmoothingMode.AntiAlias
' ' Get the largest prices.
' Dim max_price As Single = 10
' For Each symbol_prices As List(Of Single) In Prices
' Dim new_max As Single = symbol_prices.Max()
' If (max_price < new_max) Then max_price = new_max
' Next symbol_prices
' ' Scale and translate the graph.
' Dim scale_x As Single = -picGraph.ClientSize.Width / CSng(Prices(0).Count)
' Dim scale_y As Single = -picGraph.ClientSize.Height / max_price
' gr.ScaleTransform(scale_x, scale_y)
' gr.TranslateTransform( _
' picGraph.ClientSize.Width, _
' picGraph.ClientSize.Height, _
' System.Drawing.Drawing2D.MatrixOrder.Append)
' ' Draw the grid lines.
' Using string_format As New StringFormat()
' Using thin_pen As New Pen(Color.Gray, 0)
' For y As Integer = 0 To CInt(max_price) Step 10
' gr.DrawLine(thin_pen, 0, y, Prices(0).Count, y)
' Next y
' For x As Integer = 0 To Prices(0).Count - 1 Step 7
' gr.DrawLine(thin_pen, x, 0, x, 2)
' Next x
' End Using
' End Using
' ' Draw each symbol's prices.
' Dim colors() As Color = {Color.Black, Color.Red, Color.Green, Color.Blue, Color.Orange, Color.Purple}
' For symbol_num As Integer = 0 To Prices.Length - 1
' Dim symbol_prices As List(Of Single) = Prices(symbol_num)
' ' Make the data points.
' Dim points(0 To symbol_prices.Count - 1) As PointF
' For i As Integer = 0 To symbol_prices.Count - 1
' points(i) = New PointF(i, symbol_prices(i))
' Next i
' ' Draw the points.
' Dim clr As Color = colors(symbol_num Mod colors.Length)
' Using thin_pen As New Pen(clr, 0)
' gr.DrawLines(thin_pen, points)
' End Using
' ' Draw the symbol's name.
' DrawSymbolName(gr, Symbols(symbol_num), _
' symbol_prices(symbol_prices.Count - 1), clr)
' Next symbol_num
' End Using
' ' Display the result.
' picGraph.Image = bm
'End Sub
' Draw the text at the specified location.
'Private Sub DrawSymbolName(ByVal gr As Graphics, ByVal txt As String, ByVal y As Single, ByVal clr As Color)
' ' See where the point is in PictureBox coordinates.
' Dim old_transformation As Matrix = gr.Transform
' Dim pt() As PointF = {New PointF(0, y)}
' gr.Transform.TransformPoints(pt)
' ' Reset the transformation.
' gr.ResetTransform()
' ' Draw the text.
' Using small_font As New Font("Arial", 8)
' Using br As New SolidBrush(clr)
' gr.DrawString(txt, small_font, br, 0, pt(0).Y)
' End Using
' End Using
' ' Restore the original transformation.
' gr.Transform = old_transformation
'End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
导入系统.Net
导入System.IO
导入System.Drawing.Drawing2D
公开课表格1
“股票代码。
私有符号作为列表(字符串)=无
"目前的价格,。
私人价格()作为列表(单个)=无
'重新绘制图表。
'专用子图片_Resize(ByVal sender作为对象,ByVal e作为System.EventArgs)
'DrawGraph()
'末端接头
获取收盘价并绘制图表。
私有子btnGo_Click(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理btnGo。单击
Me.Cursor=Cursors.WaitCursor
'获取股票代码。
“Dim symbols_text()作为字符串=txtSymbols.text.Split(“,”c)
Dim symbols_text()作为字符串={“PKG”、“TEN”、“INDU”}
符号=新列表(字符串)()
对于i,整数=0到符号\u text.Length-1
Symbols.Add(Symbols\u text(i).Trim())
接下来我
“获取数据。
重拨价格(0到符号。计数-1)
对于i,整数=0到符号。计数-1
价格(i)=股票价格(符号(i))
接下来我
”他说。
'DrawGraph()
FillData()
Me.Cursor=Cursors.Default
端接头
'获取此符号的价格。
'私有函数GetStockPrices(ByVal符号作为字符串)作为列表(单个)
“”编写URL。
'将url设置为字符串='http://www.google.com/finance/historical?output=csv&q=“&符号
“”获取结果。
“”获取web响应。
'Dim结果为字符串=GetWebResponse(url)
“”获取历史价格。
'Dim lines()作为字符串=result.Split(_
'新字符串(){vbCr,vbLf}_
'StringSplitOptions.RemoveEmptyEntries)
"新单张(单张)(单张)价格较低()
“”处理行,跳过标头。
'对于i,整数=1到行。长度-1
'作为字符串的尺寸线=线(i)
'prices.Add(Single.Parse(line.Split(“,”c)(4)))
“接下来我
“返回价格
'结束函数
公共函数GetStockPrices(符号为字符串)作为列表(单个)
'Dim pricesArray()作为字符串
作为新列表(单张)的Dim价格()
Dim items()作为GoogleFinanceItem
如果UCase(符号)=“印度”,则
将url设置为字符串=”http://finance.google.com/finance/info?client=ig&q=INDEXDJX%3A.DJI"
Dim结果为String=GetWebResponse(url)
'Dim newResult=result.Substring(1,result.Length-1)
items=JsonConvert.DeserializeObject(属于GoogleFinanceItem())(结果)
'pricesArray=Split(结果:“”)
其他的
将url设置为字符串=”http://finance.google.com/finance/info?client=ig&q=“&UCase(符号)
Dim结果为String=GetWebResponse(url)
'Dim newResult=result.Substring(1,result.Length-1)
items=JsonConvert.DeserializeObject(属于GoogleFinanceItem())(结果)
'pricesArray=Split(结果:“”)
如果结束
'pricesArray=Split(pricesArray(4),“”)
'prices.Add(CSng(格式:pricesArray(1),“0.00”))
'prices.Add(CSng(格式为pricesArray(1)))
价格。添加(CSng(第(0)项价格))
退货价格
端函数
'获取web响应。
私有函数GetWebResponse(ByVal url作为字符串)作为字符串
'创建一个网络客户端。
Dim web_客户端作为新的WebClient()
'获取指定的URL。
Dim响应为Stream=web_client.OpenRead(url)
“阅读结果。
将stream_reader用作新的StreamReader(响应)
“得到结果。
Dim结果为String=stream\u reader.ReadToEnd()
'关闭流读取器及其底层流。
stream_reader.Close()
'返回结果。
返回结果
终端使用
端函数
私有子数据()
Dim symbolSubSet()作为控件={Label1,Label2,Label3}
Dim priceSubSet()作为控件={Label4,Label5,Label6}
对于i=0到2
symbolSubSet(i).文本=符号(i)
priceSubSet(i).Text=价格(i).ToString
下一个
端接头
“画图表。
'专用子图形()
“如果(价格不算什么),那么返回
“”生成位图。
'将bm变暗为新位图(_
'picGraph.ClientSize.Width_
'picGraph.ClientSize.Height)
'使用gr作为图形=Graphics.FromImage(bm)
'gr.Clear(颜色:白色)
'gr.SmoothingMode=SmoothingMode.AntiAlias
“价格最高。
“单件尺寸最大价格=10”
'对于每个符号(单个的)价格列表
'Dim new_max As Single=symbol_prices.max()
'如果(最大价格<新价格),则最大价格=新价格
“下一个符号”\u价格
“”缩放并转换图形。
'Dim scale_x As Single=-picGraph.ClientSize.Width/CSng(价格(0).Count)
'Dim scale_y As Single=-picGraph.ClientSize.Height/max_price
'gr.ScaleTransform(缩放x,缩放y)
'gr.TranslateTransform(_
'picGraph.ClientSize.Width_
'picGraph.ClientSize.Height_
'System.Drawing.Drawing2D.MatrixOrder.Append)
“”绘制网格线。
'将字符串_格式用作新的StringFormat()
'使用细笔作为新笔(Color.Gray,0)
'对于y作为整数=0到CInt(最大价格)步骤10
'gr.抽绳(薄笔,0,y,价格(0)。计数,y)
“下一个是y
'对于x为整数=0的价格(0)。计数-1步骤7
'gr.抽绳(细笔,x,0,x,2)
“下一个x
"终端使用",
"终端使用",
“”绘制每个符号的价格。
“暗淡的
Imports Newtonsoft.Json
Public Class GoogleFinanceItem
Public Property id As String
Public Property t As String
Public Property e As String
Public Property l As String
<JsonProperty("l_fix")>
Public Property price As String
Public Property l_cur As String
Public Property s As String
Public Property ltt As String
Public Property lt As String
Public Property lt_dts As Date
Public Property c As String
Public Property c_fix As String
Public Property cp As String
Public Property cp_fix As String
Public Property ccol As String
Public Property pcls_fix As String
End Class
' see this link for the meaning of the other properties
'https://www.quora.com/What-do-the-following-attributes-returned-in-a-JSON-array-from-the-Google-Finance-API-mean
Public Class GoogleFinanceItem
Public Property id As String
<JsonProperty("t")>
Public Property Symbol As String
<JsonProperty("e")>
Public Property Index As String
<JsonProperty("l")>
Public Property LastTradePrice As Decimal
<JsonProperty("l_fix")>
Public Property LastPriceCurrency As Decimal
Public Property l_cur As String
Public Property s As String
Public Property ltt As String
Public Property lt As String
Public Property lt_dts As Date
<JsonProperty("c")>
Public Property Change As String
Public Property c_fix As String
Public Property cp As String
Public Property cp_fix As Decimal
Public Property ccol As String
Public Property pcls_fix As String
End Class
Dim jstr = GetWebResponse(url).Replace("//", "")
Dim items As GoogleFinanceItem() = JsonConvert.DeserializeObject(Of GoogleFinanceItem())(jstr)
Console.Writeline(items(0).Ticker))
Dim syms As String() = {"GOOG", "MSFT", "INTC"}
GQuotes = New List(Of GoogleFinanceItem)
Dim urlMask As String = "http://finance.google.com/finance/info?client=ig&q={0}"
Dim url As String
For Each s As String In syms
url = String.Format(urlMask, s)
Dim jstr As String = GetWebResponse(url).Replace("//", "")
Dim items = JsonConvert.DeserializeObject(Of GoogleFinanceItem())(jstr)
GQuotes.Add(items(0))
Next
dgv1.DataSource = GQuotes