parse a json

i have used the following code,
Dim exampleJson As String = TextBox2.Text
Dim tempPost = New With {Key .balance = ""}
Dim post = JsonConvert.DeserializeAnonymousType(exampleJson, tempPost)
Dim com As String = post.balance
TextBox3.Text = com

textbox2 contains following values
{"balance":{"balance":"3.63","currency":"USD","loginid":""},"echo_req":{"balance":1},"msg_type":"balance"}

but textbox3 doesn't return any value.

please help me to parse the balance amount i.e 3.63 in textbox3

Comments

  • post.balance will give you {"balance":"3.63","currency":"USD","loginid":""} to get 3.63 you need to do post.balance.balance

  • it shows the following error

  • edited March 2017

    post.balance is actually not returning anything to me but you have said it has to return

    {"balance":"3.63","currency":"USD","loginid":""} but it is not returning anything on textbox3

  • @mahendran1992 your post is json object, hence you can do this post.balance to access balance key, but you are assigning post.balance to string Dim com As String = post.balance hence you can not do com.balance so better convert post.balance to json object and then you can get balance from it

  • i have pasted my full code snippet

    Imports Newtonsoft.Json
    Imports Newtonsoft
    Imports WebSocket4Net
    Imports Newtonsoft.Json.Linq
    
    Public Class Form1
          Private websocket2 As WebSocket4Net.WebSocket
    
        Private Sub Button1_Click(sender As Object, ex As EventArgs) Handles Button1.Click
    
            websocket2 = New WebSocket4Net.WebSocket("wss://ws.binaryws.com/websockets/v3?app_id=xxxx")
            AddHandler websocket2.Opened, Sub(s, e) socketOpened2(s, e)
            AddHandler websocket2.Error, Sub(s, e) socketError2(s, e)
            AddHandler websocket2.Closed, Sub(s, e) socketClosed2(s, e)
            AddHandler websocket2.MessageReceived, Sub(s, e) socketMessage2(s, e)
            AddHandler websocket2.DataReceived, Sub(s, e) socketDataReceived2(s, e)
    
            websocket2.Open()
        End Sub
    
        Sub socketOpened2(ByVal s As Object, ByVal e As EventArgs)
    
            websocket2.Send("{""authorize"":""xxxxxxxxxxxxxxxx""}")
             websocket2.Send("{""balance"": 1}")
    
        End Sub
    
        Sub socketClosed2(ByVal s As Object, ByVal e As EventArgs)
            MsgBox("close")
        End Sub
    
        Sub socketError2(ByVal s As Object, ByVal e As SuperSocket.ClientEngine.ErrorEventArgs)
            MsgBox("error")
        End Sub
    
        Sub socketMessage2(ByVal s As Object, ByVal e As WebSocket4Net.MessageReceivedEventArgs)
              TextBox2.Text = (e.Message.ToString())
    
    
        End Sub
    
    
        Sub socketDataReceived2(ByVal ss As Object, ByVal e As WebSocket4Net.DataReceivedEventArgs)
    
        End Sub
    
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Try
                Dim exampleJson As String = TextBox2.Text
                Dim tempPost = New With {Key .balance = ""}
                Dim post = JsonConvert.DeserializeAnonymousType(exampleJson, tempPost)
                Textbox3.text=post.balance //It's not returning any value
            Catch ex As Exception
    
            End Try
        End Sub
    End Class
    
  • please find the error or give me any example program to parse the balance in Visual basic 2013

Sign In or Register to comment.