Android Question parse a JSON string

Discussion in 'Android Questions' started by Elton Leung, Sep 21, 2015.

  1. Elton Leung

    Elton Leung Member Licensed 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:
    Code:
    {"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. :)
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. Elton Leung

    Elton Leung Member Licensed 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.


    Code:
    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?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    You will need to write these three lines.
     
  5. Elton Leung

    Elton Leung Member Licensed 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.

    Code:
    {"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"}
     
  6. inakigarm

    inakigarm Well-Known Member Licensed User

    If you try the online JSON parser http://b4x.com:51042/json/index.html, it will show you the needed code

    If you had tried before and have any problems with your code, please attach your code and logs
     
    DonManfred, cyiwin and Elton Leung like this.
  7. Elton Leung

    Elton Leung Member Licensed User


    wow, that parser is special one specially for B4A, I thought it just just another online parser. Thanks !!!!
     
  8. DonManfred

    DonManfred Expert Licensed User

    AFAIK not just b4a; it´s for b4x i believe. The generated code will work for B4A, B4J and i think B4i too.
     
    Erel likes this.
  9. Erel

    Erel Administrator Staff Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice