Android Question json parsing problem

tufanv

Expert
Licensed User
Longtime User
Hello,

I am using Erel's online json tool to get the json code easily . my received json is stg like this :

B4X:
{"BTC_BCN":{"id":7,"last":"0.00000026","lowestAsk":"0.00000027","highestBid":"0.00000026","percentChange":"0.08333333","baseVolume":"43.73921153","quoteVolume":"172390206.07905212","isFrozen":"0","high24hr":"0.00000027","low24hr":"0.00000024"},"BTC_BELA":{"id":8,"last":"0.00001774","lowestAsk":"0.00001774","highestBid":"0.00001772","percentChange":"-0.02954048","baseVolume":"4.12240710","quoteVolume":"220808.00322701","isFrozen":"0","high24hr":"0.00001933","low24hr":"0.00001732"}}

this goes long for many other symbols like BTC_BCN , BTC_BELA etc ...
The json tool gives me the following code :

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim BTC_BCN As Map = root.Get("BTC_BCN")
Dim percentChange As String = BTC_BCN.Get("percentChange")
Dim high24hr As String = BTC_BCN.Get("high24hr")
Dim last As String = BTC_BCN.Get("last")
Dim highestBid As String = BTC_BCN.Get("highestBid")
Dim id As Int = BTC_BCN.Get("id")
Dim quoteVolume As String = BTC_BCN.Get("quoteVolume")
Dim baseVolume As String = BTC_BCN.Get("baseVolume")
Dim isFrozen As String = BTC_BCN.Get("isFrozen")
Dim lowestAsk As String = BTC_BCN.Get("lowestAsk")
Dim low24hr As String = BTC_BCN.Get("low24hr")
Dim BTC_BELA As Map = root.Get("BTC_BELA")
Dim percentChange As String = BTC_BELA.Get("percentChange")
Dim high24hr As String = BTC_BELA.Get("high24hr")
Dim last As String = BTC_BELA.Get("last")
Dim highestBid As String = BTC_BELA.Get("highestBid")
Dim id As Int = BTC_BELA.Get("id")
Dim quoteVolume As String = BTC_BELA.Get("quoteVolume")
Dim baseVolume As String = BTC_BELA.Get("baseVolume")
Dim isFrozen As String = BTC_BELA.Get("isFrozen")
Dim lowestAsk As String = BTC_BELA.Get("lowestAsk")
Dim low24hr As String = BTC_BELA.Get("low24hr")

problem for me here is I don't want to use dim BTC_BCN as map ..... because ther eare more than 2000 symbols like this and if i use this ode there will be many many lines. Is there a way to get those symbol names only with a different code. I only want to get the symbols like BTC_BCN , BTC_BELA etc...

if i log the root it gives

B4X:
(MyMap) {BTC_FLDC={highestBid=0.00000156, id=31, low24hr=0.00000152, last=0.00000157, high24hr=0.00000162, percentChange=-0.00632911, isFrozen=0, lowestAsk=0.00000157, quoteVolume=4880668.59574651, baseVolume=7.60407161},

.... goes like this . I couldnt find a way to get the symbol only. Can you help it out. It must be easy but i cant get it.
 

tufanv

Expert
Licensed User
Longtime User
The online json tool shows you how to extract all the available information. You can and should remove all the code that extracts fields that you don't need.
I know that and i deleted everything but i cant extract the symbol names because tool declares them like dim btc_bcn as map and many other symbols like this, but i cant declare them i need them from the json
 
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
use this:
B4X:
Sub parseJson
    Dim parser As JSONParser
    parser.Initialize( getJson)
    Dim root As Map = parser.NextObject
    For Each k As String In root.Keys
        Log(k)
    Next
End Sub
Sub getJson As String
    Return File.ReadString( File.DirApp, "json.txt")
End Sub
results:
Waiting for debugger to connect...
Program started.
BTC_BCN
BTC_BELA
 
Upvote 0

wonder

Expert
Licensed User
Longtime User
@tufanv, I don't know if this is what you're looking for, but I hope it helps! :)

EDIT: I deleted the previous screenshot and modified the project. My previous code was wrong.

Correct:
upload_2017-10-19_14-35-28.png
 

Attachments

  • json_is_cool.zip
    1.5 KB · Views: 301
Last edited:
Upvote 0

tufanv

Expert
Licensed User
Longtime User
I can't find the like button because of the pic corrupts my view but thanks @wonder !

I have another problem similar to this , I use an api to get account balance. If the account balance is empty , returned response is like this ( here only BTC_XRP is not empty so it has some data in it:

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim ETH_GNT As List = root.Get("ETH_GNT")
Dim BTC_BELA As List = root.Get("BTC_BELA")
Dim BTC_PASC As List = root.Get("BTC_PASC")
Dim BTC_RADS As List = root.Get("BTC_RADS")
Dim BTC_XBC As List = root.Get("BTC_XBC")
Dim ETH_GNO As List = root.Get("ETH_GNO")
Dim BTC_XRP As List = root.Get("BTC_XRP")
For Each colBTC_XRP As Map In BTC_XRP
Dim date As String = colBTC_XRP.Get("date")
Dim amount As String = colBTC_XRP.Get("amount")
Dim total As String = colBTC_XRP.Get("total")
Dim margin As Int = colBTC_XRP.Get("margin")
Dim orderNumber As String = colBTC_XRP.Get("orderNumber")
Dim rate As String = colBTC_XRP.Get("rate")
Dim startingAmount As String = colBTC_XRP.Get("startingAmount")
Dim type As String = colBTC_XRP.Get("type")
Next
Dim BTC_GAME As List = root.Get("BTC_GAME")
Dim BTC_EMC2 As List = root.Get("BTC_EMC2")

if the user has made an order for BTC_PASC for example , it will also return like BTC_XRP as bold code.
How can i programaticly do this ? I mean If I put this code into my app, and if the user has BTC_GNT order, It wont show the details , What I need is , if the list is not empty for each currency , I have to get details with map but it is empty it is not needed. I am lost in this. there are at leas 500 pairs like this so I need to identify not empty ones and declare a map and get details but how ?
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
I used the online tool once but now I do the following (it's easier than it may seem).

1. You get a JSON formatted string
2. If it starts with a [, it's a list, so you get the content with parser.NextArray
3. If it starts with a {, it's a map, so you get the content with parser.NextObject
4. If you are unsure if a key-pair exists, check it with "Map.ContainsKey"
5. Using "FOR... EACH..." may look simpler but sometimes it is not.
6. If it's nested (like lists containing maps containing lists with maps or maps containing maps) this works, too
7. You can check for "[" and "{" to see if it's a map or a list
8. In the end there's no "ready to run code" because you need to know what to do with the extracted data

How can i programaticly do this ?

Can you post the "problematic" JSON string?
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
I used the online tool once but now I do the following (it's easier than it may seem).

1. You get a JSON formatted string
2. If it starts with a [, it's a list, so you get the content with parser.NextArray
3. If it starts with a {, it's a map, so you get the content with parser.NextObject
4. If you are unsure if a key-pair exists, check it with "Map.ContainsKey"
5. Using "FOR... EACH..." may look simpler but sometimes it is not.
6. If it's nested (like lists containing maps containing lists with maps or maps containing maps) this works, too
7. You can check for "[" and "{" to see if it's a map or a list
8. In the end there's no "ready to run code" because you need to know what to do with the extracted data



Can you post the "problematic" JSON string?
Hi,

Thanks for the tips !
This is the json string :
B4X:
{"BTC_AMP":[],"BTC_ARDR":[],"BTC_BCH":[],"BTC_BCN":[],"BTC_BCY":[],"BTC_BELA":[],"BTC_BLK":[],"BTC_BTCD":[],"BTC_BTM":[],"BTC_BTS":[],"BTC_BURST":[],"BTC_CLAM":[],"BTC_CVC":[],"BTC_DASH":[],"BTC_DCR":[],"BTC_DGB":[],"BTC_DOGE":[],"BTC_EMC2":[],"BTC_ETC":[],"BTC_ETH":[],"BTC_EXP":[],"BTC_FCT":[],"BTC_FLDC":[],"BTC_FLO":[],"BTC_GAME":[],"BTC_GAS":[],"BTC_GNO":[],"BTC_GNT":[],"BTC_GRC":[],"BTC_HUC":[],"BTC_LBC":[],"BTC_LSK":[],"BTC_LTC":[],"BTC_MAID":[],"BTC_NAUT":[],"BTC_NAV":[],"BTC_NEOS":[],"BTC_NMC":[],"BTC_NOTE":[],"BTC_NXC":[],"BTC_NXT":[],"BTC_OMG":[],"BTC_OMNI":[],"BTC_PASC":[],"BTC_PINK":[],"BTC_POT":[],"BTC_PPC":[],"BTC_RADS":[],"BTC_REP":[],"BTC_RIC":[],"BTC_SBD":[],"BTC_SC":[],"BTC_SJCX":[],"BTC_STEEM":[],"BTC_STR":[],"BTC_STRAT":[],"BTC_SYS":[],"BTC_VIA":[],"BTC_VRC":[],"BTC_VTC":[],"BTC_XBC":[],"BTC_XCP":[],"BTC_XEM":[],"BTC_XMR":[],"BTC_XPM":[],"BTC_XRP":[{"orderNumber":"85613396022","type":"sell","rate":"0.00004500","startingAmount":"17.00000000","amount":"17.00000000","total":"0.00076500","date":"2017-10-23 07:22:57","margin":0}],"BTC_XVC":[],"BTC_ZEC":[],"BTC_ZRX":[],"ETH_BCH":[],"ETH_CVC":[],"ETH_ETC":[],"ETH_GAS":[],"ETH_GNO":[],"ETH_GNT":[],"ETH_LSK":[],"ETH_OMG":[],"ETH_REP":[],"ETH_STEEM":[],"ETH_ZEC":[],"ETH_ZRX":[],"USDT_BCH":[],"USDT_BTC":[],"USDT_DASH":[],"USDT_ETC":[],"USDT_ETH":[],"USDT_LTC":[],"USDT_NXT":[],"USDT_REP":[],"USDT_STR":[],"USDT_XMR":[],"USDT_XRP":[],"USDT_ZEC":[],"XMR_BCN":[],"XMR_BLK":[],"XMR_BTCD":[],"XMR_DASH":[],"XMR_LTC":[],"XMR_MAID":[],"XMR_NXT":[],"XMR_ZEC":[]}

problem is it changes from user to user , I dont know which user has a open order in which of the pair so I need to identify which pair is not empty and later get its details . ( here the user only has btc_xrp but it can be different for another user )
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Why do you have so many lists? Why not utilize a single map object and navigate it?
That's the reason I created the helper function.
this is an api, I want to list open orders of users , api return getopenorders requests for every pair available on their list. If the user has no open order , still all the pairs returned but empty, I just need to identify not empty ones and get their info. I couldnt understand if thecode you posted does this .
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
ok i did this like this and worked :
B4X:
            For Each key In root22.Keys
                    sik=key
                    Dim abbas As List = root22.Get(sik)
'                sik=sik.Replace("_","-")
                    If abbas.Size=0 Then
                    Else

                       
                        For Each mahmut As Map In abbas
                            Dim amount As String = mahmut.Get("amount")
                            LogColor(sik & "  " &amount,Colors.red)
                        Next
                       
                       
                  
                   End If
                Next
 
Upvote 0
Top