Click here to Skip to main content
14,486,704 members
Rate this:
Please Sign up or sign in to vote.
See more:
Great programmers, i am having a hard time trying to parse a Json string so as to extract from it some value. i really need this because i want to include it in my app.
Please help me out.. i am just new to JSON parsing because of this..
I want to extract the value of "address" , "txes_total", "confirmed".
Everything i tried did not work even copying code from youtube and through google search yield no result.
Here is the JSON code:

{
  "data": {
    "address": "AnyRandomString", 
    "balance": {
      "confirmed": 960, 
      "unconfirmed": 960
    }, 
    "time_last_tx": 188.473, 
    "txes_total": 2
  }


}


What I have tried:

Dim str As String = reader.ReadToEnd()
Dim dict As Object = New JavaScriptSerializer().Deserialize(Of List(Of Object))(str)
Dim BITCONIadress As String = Nothing
Dim transactmBiTCOIN As Integer = Nothing
Dim balanceBBITCOIn As String = Nothing
For Each itm As Object In dict
    BITCONIadress = dict("address").ToString()
    transactmBiTCOIN = CInt(dict("txes_total"))
    balanceBBITCOIn = dict("confirmed").ToString()
Next
Dim kConvert = Convert.ToDecimal(balanceBBITCOIn) / 100000000
Threading.Thread.Sleep(3)
lll += 1
Me.Invoke(New MethodInvoker(Sub() Me.label7.Text = lll & "/" & countii))
Dim lstv As New ListViewItem
    lstv.Text = BITCONIadress
    lstv.SubItems.Add(ListBox2.Items.Item(lstcnt))
    lstv.SubItems.Add(transactmBiTCOIN)
    listView1.Items.Add(lstv)
    Me.Invoke(New MethodInvoker(Sub() Me.label8.Text = listView1.Items.Count))
    ListBox1.Items.RemoveAt(lstcnt)
    ListBox2.Items.RemoveAt(lstcnt)
    lstcnt += 1
Posted
Updated 23-Mar-20 8:22am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Instead of manually parsing this; why don't you look at what you have, and then use the properties to get what you need.

In this case, you have JSON. This is an object that has been serialized into a string format.
What I would do is:
1. Create a class with a design parallel to your JSON.
2. DeSerialize the JSON into instances of that class (objects)
3. Read the desired properties from those instances.

References:
Deserialize an Object[^]
How to serialize and deserialize JSON using C# - .NET | Microsoft Docs[^]
   
Comments
Member 10285969 23-Mar-20 15:50pm
   
can you show me a sample using my json requiements? pleasee
MadMyche 23-Mar-20 16:18pm
   
I can help you with specific problems, but I am not going to write it all out.

I would use an online tool to create the classes from the JSON, I use this one for C# which you should be able to trans-code into VB
http://json2csharp.com/

Once you got that worked out, go through the samples/tutorials from my answer; and then start working what you've learned into what you need.
Member 10285969 24-Mar-20 0:13am
   
Thanks for pointing me to the right Question to ask inorder to understand this better. but the links you give was not too simple for me to understand but i used your methods to know what to search for in youtube and this video got me what i wanted.. "[VB.NET] Visual Basic Project - Weather App via JSON API Response.mp4"
it talks about the <serializable> and <serializableattribute> which i was able to use with the link you gave when i convert it back to vb.net "http://json2csharp.com/"
MadMyche 24-Mar-20 7:39am
   
I hoped it help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100