Android Question json help

tufanv

Expert
Licensed User
Longtime User
Hello

I have a json retrieved from remote web
B4X:
{"time":{"updated":"Dec 19, 2016 12:30:00 UTC","updatedISO":"2016-12-19T12:30:00+00:00","updateduk":"Dec 19, 2016 at 12:30 GMT"},"bpi":{"USD":{"code":"USD","rate":"791.3488","description":"United States Dollar","rate_float":791.3488},"TRY":{"code":"TRY","rate":"2,770.3276","description":"Turkish Lira","rate_float":2770.3276}}}

I got this with httpjob and used the code below :
B4X:
Dim parser As JSONParser
parser.Initialize(res)
dim m as map = parser.nextobject

log m gives me :

B4X:
    time = "(read only map) {\n    updated = \"Dec 19, 2016 12:34:00 UTC\";\n    updatedISO = \"2016-12-19T12:34:00+00:00\";\n    updateduk = \"Dec 19, 2016 at 12:34 GMT\";\n}";
}
{"time":{"updated":"Dec 19, 2016 12:34:00 UTC","updatedISO":"2016-12-19T12:34:00+00:00","updateduk":"Dec 19, 2016 at 12:34 GMT"},"bpi":{"USD":{"code":"USD","rate":"791.0175","description":"United States Dollar","rate_float":791.0175},"TRY":{"code":"TRY","rate":"2,770.3276","description":"Turkish Lira","rate_float":2770.3276}}}
code":"TRY","rate":"2,770.3276","description"

How can i retrieve the "rate_float" from this map ? m.get("rate_float") is not working.

TY
 

Mahares

Expert
Licensed User
Longtime User
This is just a map inside a map -> brackets {} = maps. So let's follow this thought:
Nice tutorial.
in case someone needs in the future , I solved id with the code below :
Here is an alternate solution to your case. Use a list to sort the keys. It is NOT necessarily better than yours. It just avoids date manipulations:
B4X:
Dim MyList As List
    MyList.Initialize
    Dim parser As JSONParser
    parser.Initialize(res)
    Dim root3 As Map = parser.NextObject
    Dim bpi As Map = root3.Get("bpi")
    For i=0 To bpi.Size-1
        MyList.Add(bpi.GetKeyAt(i))
    Next
    MyList.Sort(True)
    For i=0 To MyList.Size-1
        Log(bpi.Get(MyList.Get(i)))  'the values will be displayed by ascending dates
    Next
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Note that you should avoid using GetKeyAt. It is deprecated, not cross platform compatible
It is becoming apparent that many keywords, methods, properties, etc. are or will be deprecated for the sake of cross platform compatibility or other reasons, namely GetKeyAt. Is it possible for @Erel to create a new sub forum or section to deal with it and point us there. I do not think it is enough to search for 'deprecated'. I have a feeling some day 'Cursor' may also be deprecated for the benefit of 'Resultset' if we are heading toward cross platform compatibility.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. GetKeyAt / GetValueAt will never be removed. You can continue to use them. They were added before the For Each iterator was available to allow iterating over the Map items. The For Each iterator is better in this case.
2. Cursor is not deprecated.

I plan to add a warning for deprecated methods. There are very few deprecated methods in B4A (maybe 5 methods).
 
Upvote 0
Top