Google Financial JSON 문자열 역 직렬화

에드가

3 개 회사의 최신 주가를 파악하려고하는데 정확한 수치를 얻을 수없는 것 같습니다. Google 금융을 긁어 모은 기호를 얻을 수 있으며 다음과 유사한 개체를 얻을 수 있습니다.

 // [ { "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" } ] 

네 번째 콜론 인 "18,051.10"다음에 항목을 잡고 나머지는 무효화하고 이중 값을 배열에 넣어서 반환하고 레이블에 표시 할 수는 있지만 운이 없습니다. 저는 회사의 주식 그래프 기록을 반환하는 샘플 프로그램을 연습하려고하는 멍청한 프로그래머이지만 최신 견적을 반환하고 싶습니다.

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

GoogleFinanceItem 클래스도 있습니다.

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

나는 json을 deserialize하려고하는데 json 문자열을 결과에 덤프하고 deserializer에 전달하고 다음 오류가 발생합니다.

Newtonsoft.Json.dll에서 'Newtonsoft.Json.JsonSerializationException'유형의 처리되지 않은 예외가 발생했습니다.

추가 정보 : 올바른 역 직렬화를 위해 JSON 배열 (예 : [1,2,3])이 필요하므로 현재 JSON 개체 (예 : { "name": "value"})를 'howto_net_graph_stock_history.GoogleFinanceItem []'형식으로 역 직렬화 할 수 없습니다. .

이 오류를 수정하려면 JSON을 JSON 배열 (예 : [1,2,3])로 변경하거나 deserialized 유형을 변경하여 일반 .NET 유형이되도록 변경하십시오 (예 : 정수와 같은 기본 유형이 아니라 다음과 같은 컬렉션 유형이 아님). JSON 개체에서 역 직렬화 할 수있는 배열 또는 목록)입니다. JsonObjectAttribute를 유형에 추가하여 JSON 객체에서 강제로 역 직렬화 할 수도 있습니다.

여기에 이미지 설명 입력

Pssa Pøngjǣrdenlarp

그것은 멋진 형식의 문자열이 아닙니다. 이는 모든 정보에 쉽게 액세스 할 수 있도록 객체로 구문 분석하거나 역 직렬화 할 수 있음을 의미하는 JSON 문자열입니다.

먼저 Newtonsoft의 JSON 파서 를 얻고 싶습니다 . 그런 다음 데이터를 캡처하는 클래스 :

' 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

다음과 같이 클래스를 자동 생성 할 수 있습니다.

  1. json을 클립 보드에 복사
  2. 새 클래스 파일 만들기
  3. 선택 편집 | 선택하여 붙여 넣기 | JSON을 클래스로 붙여 넣기

이전 버전의 Visual Studio가없는 경우 http://jsonutils.com/ 에서 로봇을 사용하여 VB 또는 C # 클래스를 만들 수 있습니다.

로봇이 제안한 클래스를 약간 변경했습니다. a) 꽤 지저분한 속성 이름입니다. 좀 더 설명 <JsonProperty("json name")>적이 거나 읽기 쉬운 것으로 변환하려면을 사용 하여 JSON.NET에 다른 속성 이름으로 역 직렬화 하도록 지시 할 수 있습니다 . b) 또한 숫자를으로 변경하여 Decimal다른 곳에서 수학에서 사용할 수 있습니다.

역 직렬화는 쉽지만 추가 정리 단계가 있습니다. Google은 응답을 직접 사용하는 것보다 API를 사용하는 것을 선호하므로 앞에 "//"가 있습니다. 이것은 deserialize하기 전에 잘라야합니다.

Dim jstr = GetWebResponse(url).Replace("//", "")

Dim items As GoogleFinanceItem() = JsonConvert.DeserializeObject(Of GoogleFinanceItem())(jstr)

이 경우 하나의 배열이지만 속성을 통해 데이터를 사용할 수 있습니다.

Console.Writeline(items(0).Ticker))

속성 그리드를 사용하여 객체의 속성 값보기 :

여기에 이미지 설명 입력


미래의 독자들은 DGV 목록에 여러 개의 따옴표를 포함 할 수 있습니다. API 외부에서 하나의 요청으로 여러 개의 견적을 요청하는 방법이 있는지 여부는 명확하지 않습니다. 따라서 루프를 사용하십시오.

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

목록이 길면 너무 짧은 시간에 너무 많은 요청을 게시하지 않도록 요청 사이에 잠시 일시 중지해야 할 수 있습니다.

여기에 이미지 설명 입력

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

JSON 문자열 역 직렬화

분류에서Dev

배열 JSON 역 직렬화

분류에서Dev

Spring JSON 역 직렬화 문자 제한

분류에서Dev

JSON 문자열에서 선택한 JSON 개체 역 직렬화

분류에서Dev

문자열을 Google TokenResponse로 역 직렬화

분류에서Dev

Json 배열 Newtonsoft.Json 역 직렬화

분류에서Dev

json 문자열을 .NET 개체 목록으로 역 직렬화

분류에서Dev

JSON 문자열을 C # 및 Unity의 개체로 역 직렬화

분류에서Dev

AWS Lambda에서 중첩 된 JSON 문자열 역 직렬화 (C #)

분류에서Dev

C # 목록으로 JSON 문자열 역 직렬화

분류에서Dev

복잡한 동적 JSON 문자열 역 직렬화

분류에서Dev

Json 문자열이 역 직렬화되지 않습니다.

분류에서Dev

json 문자열을 개체 C # .net으로 역 직렬화

분류에서Dev

클래스에 JSON 문자열 역 직렬화

분류에서Dev

JSON 문자열을 객체로 역 직렬화하는 방법

분류에서Dev

역 직렬화를 원하지 않는 JSON 문자열

분류에서Dev

배열의 JSON 배열 역 직렬화

분류에서Dev

JSON 역 직렬화

분류에서Dev

JSON 역 직렬화 구문 분석 C #

분류에서Dev

튜플의 JSON 역 직렬화 문제?

분류에서Dev

C #에서 JSON 배열 역 직렬화

분류에서Dev

newtonsoft로 json 배열 역 직렬화

분류에서Dev

System.Text.Json 사용자 지정 직렬화 / 역 직렬화

분류에서Dev

자바 다형성 Json 역 직렬화

분류에서Dev

특수 문자가 포함 된 JSON.Net 역 직렬화 문자열

분류에서Dev

C # / JSON 개체 직렬화, 역 직렬화 및 대 / 소문자 문제 없음

분류에서Dev

JSON 문자열을 열거 형으로 역 직렬화하는 방법

분류에서Dev

Newtonsoft.JSON을 사용하여 C #으로 Json 문자열 역 직렬화

분류에서Dev

Json 스키마를 Json 문자열 또는 개체로 역 직렬화

Related 관련 기사

뜨겁다태그

보관