JSON: referencing additional levels

  1. 3 months ago

    paul t

    Jan 2 Seattle, WA

    I've got this code, which works fine. USD is on a secondary level under quote.

    Dim n As JSONItem
    Dim temp As JSONItem
    temp =  n.value("quote")
    temp = temp.value("USD")

    My question is this. Do I really need to define an additional JSONitem to gain access to the scondary level?
    Or can I combine the two lines into one statement, using only the n.value JSONItem?

    For maintainability, best practice is to take advantage of object oriented design, not try to force it into one line of code. The 1980s are gone, we can name variables with useful names and no longer really need to worry about how many we're using (until it's a massive project). Proper scoping helps maintain memory management as well, without needing to focus on it.

    This JSON reads like the way it's formatted allows the API to respond with multiple currencies. This way perhaps you could get both USD and CAD values (if the API does so). With this in mind, you would want to parse all of the objects and then find the USD object to use it. I would even make a class that does so.

    This example shows you how to do just that: json_example.xojo_binary_project
    I would consider this methodology best practices.

    If you have any questions, I'm happy to answer them - I figured it was easiest to tackle this with an example project.

  2. Tim P

    Jan 2 Pre-Release Testers feedback://46303

    It entirely depends on the JSON you're trying to parse. You may wish to review object oriented principles.
    If you provide the sample JSON, I'd be happy to help you with formulating the Xojo code to parse it :)

  3. paul t

    Jan 2 Seattle, WA

    Thanks Tim! temp = n.value("quote") returns this JSON structure:

    {"USD":{"price":3881.888646249999965,"volume_24h":6341959230.332469940185547,"percent_change_1h":0.426252,"percent_change_24h":-4.10839,"percent_change_7d":19.472000000000001,"market_cap":67684268829.814132690429688,"last_updated":"2018-12-22T06:08:23.000Z"}}

    Then... from that point, the temp = temp.value("USD") returns:

    {"price":3881.888646249999965,"volume_24h":6341959230.332469940185547,"percent_change_1h":0.426252,"percent_change_24h":-4.10839,"percent_change_7d":19.472000000000001,"market_cap":67684268829.814132690429688,"last_updated":"2018-12-22T06:08:23.000Z"}

    Using a 3rd JSONItem variable, I know I can get at the: price and volume data, but considering best practices, I was thinking I could use just the single n JSONItem, and grab the price in one line of code.

  4. Tim P

    Jan 2 Pre-Release Testers Answer feedback://46303

    For maintainability, best practice is to take advantage of object oriented design, not try to force it into one line of code. The 1980s are gone, we can name variables with useful names and no longer really need to worry about how many we're using (until it's a massive project). Proper scoping helps maintain memory management as well, without needing to focus on it.

    This JSON reads like the way it's formatted allows the API to respond with multiple currencies. This way perhaps you could get both USD and CAD values (if the API does so). With this in mind, you would want to parse all of the objects and then find the USD object to use it. I would even make a class that does so.

    This example shows you how to do just that: json_example.xojo_binary_project
    I would consider this methodology best practices.

    If you have any questions, I'm happy to answer them - I figured it was easiest to tackle this with an example project.

  5. paul t

    Jan 2 Seattle, WA

    Thanks Tim!! I appreciate the advice. And... the little example you wrote.
    I hadn't considered making it into a class. I will move my existing Methods into a class.
    And add that Try...Catch logic looking for JSONException errors.
    Interesting... how you made all the variable definitions Properties.

or Sign Up to reply!