Android Question parse a JSON string

Elton Leung

Member
Licensed User
Longtime User
Dear all,

I am trying to convert a piece of javascript I created to B4A. The javascript extract two or three variable from a JSON string which is returned from a web API.

Because of the format of the JSON string is already known, I am able to use several lines to get what I needed:

var face = JSON.parse(JSONstring));
var width = face.photos[0].width;
var height = face.photos[0].height;

Can I do that with B4A? or I have to navigate throw the string level by level to get what I needed?

Here is the JSON string:
B4X:
{"status":"success","photos":[{"url":"http://api.skybiometry.com/fc/images/get?id=XmN2X3hybD1ucTQ1NnBwMDMyNnM0cm5xbnE0NjBzOXJzNXI3bzZuMSZuY3ZfZnJwZXJnPXNxNjQ1cDUzMTg5ODQ5N285MDM1NjYybzhxbzJzbjk0JmVxPTI2MzkmY3ZxPXNvbjNxNnE3NDcxNnEmZ3Z6cmZnbnpjPTIwMTUwOTIxMDQ1NjI0","pid":"F@0c84ad51083a3de25642d1351d5e1f0e_fba3d6d74716d","width":1280,"height":720,"tags":[{"uids":[],"label":null,"confirmed":false,"manual":false,"width":21.64,"height":38.47,"yaw":-1,"roll":91,"pitch":0,"attributes":{"face":{"value":"true","confidence":71}},"points":null,"similarities":null,"tid":"TEMP_F@0c84ad51083a3de25642d135020e01d4_fba3d6d74716d_41.09_65.00_0_1","recognizable":true,"center":{"x":41.09,"y":65.0},"eye_left":{"x":45.7,"y":75.56,"confidence":59,"id":449},"eye_right":{"x":46.17,"y":55.14,"confidence":61,"id":450},"mouth_center":{"x":33.44,"y":64.86,"confidence":56,"id":615},"nose":{"x":39.92,"y":65.69,"confidence":58,"id":403}}]}],"usage":{"used":1,"remaining":99,"limit":100,"reset_time":1442807935,"reset_time_text":"Mon, 21 September 2015 03:58:55 +0000"},"operation_id":"f779562c340d4517a7e253af6b88f5ae"

thank you for your help. :)
 

Elton Leung

Member
Licensed User
Longtime User
Hi Erel,

Thank you for your prompt reply. I have been reading the tool and link that you sent me. This is why I have the impression that with B4A, I must use the .GET function to go down from one level to the one below and repeat the process in order to get information that is lower and that's why I start this thread.


B4X:
    m = Map1.Get("photos")
    m = m.Get("popup")
    MenuItems = m.Get("menuitem")

vs

var eye_left_x = face.photos[0].tags[0].eye_left.x; << from my old javascript code
Which is much more direct.

My question should be, how can I do the same thing in B4A in one line?
 
Upvote 0

Elton Leung

Member
Licensed User
Longtime User
Thanks. Can you kindly give a hand on how I can get these three items "width":1280 , "eye_left":{"x":45.7,"y":75.56

I have been trying different way of getting them from the JSON string but all I get is pause at the IDE.

B4X:
{"status":"success","photos":[{"url":"http://api.skybiometry.com/fc/images/get?id=bmN2X3hybD1ucTQ1NnBwMDMyNnM0cm5xbnE0NjBzOXJzNXI3bzZuMSZuY3ZfZnJwZXJnPXNxNjQ1cDUzMTg5ODQ5N285MDM1NjYybzhxbzJzbjk0JmVxPTI2MzkmY3ZxPXNvbjNxNnE3NDcxNnEmZ3Z6cmZnbnpjPTIwMTUwOTIxMDQ1NjI0","pid":"F@0c84ad51083a3de25642d1351d5e1f0e_fba3d6d74716d","width":1280,"height":720,"tags":[{"uids":[],"label":null,"confirmed":false,"manual":false,"width":21.64,"height":38.47,"yaw":-1,"roll":91,"pitch":0,"attributes":{"face":{"value":"true","confidence":71}},"points":null,"similarities":null,"tid":"TEMP_F@0c84ad51083a3de25642d135020e01d4_fba3d6d74716d_41.09_65.00_0_1","recognizable":true,"center":{"x":41.09,"y":65.0},"eye_left":{"x":45.7,"y":75.56,"confidence":59,"id":449},"eye_right":{"x":46.17,"y":55.14,"confidence":61,"id":450},"mouth_center":{"x":33.44,"y":64.86,"confidence":56,"id":615},"nose":{"x":39.92,"y":65.69,"confidence":58,"id":403}}]}],"usage":{"used":1,"remaining":99,"limit":100,"reset_time":1442807935,"reset_time_text":"Mon, 21 September 2015 03:58:55 +0000"},"operation_id":"f779562c340d4517a7e253af6b88f5ae"}
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0
Top