LC8 - Stumbling into JSON

Sannyasin Brahmanathaswami brahma at hindu.org
Mon Jun 13 18:30:34 EDT 2016


Not to trivialize the opacity of these things, but I used to resist JSON, but actually you can learn it in 10 minutes

http://json.org/

It's actually much easier to grok in some ways than XML and now I prefer it to XML

Sure there are deeper, more complex levels, but at a very simple level:

This is json

fruitColors {
"apples":"red",
"oranges": "orange",
"bananas","yellow"
}

So LC will return this as an array

put fruitColors["apples']

# you will get value "red"

For this BMI calculator, that's all JSON you need to know.

http://api.clinicalcalculator.org/bmi?height_in_m=1.778&weight_in_kg=68

returns a 2 dimensional array, 2  elements, each of which contains 2 elements

{"result":
    {"value":18.75,
    "units":"kg/m^2"
    },
"request_fields":
    {"height_in_m":"2",
    "weight_in_kg":"75"
         }
    }

Graham wrote: "Where did the names ‘result’ and ‘value’ come from?"

They came from the first JSON name/value pair
Hope that helps

BR




From: use-livecode <use-livecode-bounces at lists.runrev.com> on behalf of Graham Samuel <livfoss at mac.com>
Reply-To: How LiveCode <use-livecode at lists.runrev.com>
Date: Monday, June 13, 2016 at 12:08 PM
To: How LiveCode <use-livecode at lists.runrev.com>
Subject: LC8 - Stumbling into JSON

Sorry to be a pain, but again in the BMI example for LC 8 there is this function:

function getBMI pHeight, pWeight
    put "http://api.clinicalcalculator.org/bmi?" &  "height_in_m=" & pHeight & "&weight_in_kg=" & pWeight into tURL
    put url tURL into tJSON
    put JsonImport(tJSON) into tArray
    return round(tArray["result"]["value"])
end getBMI

Knowing nothing about JSON, I find this confusing to say the least. I see that the JsonImport uses a URL to get some value(s) based on parameters incorporated in the URL, but what am I to make of the ‘return’ statement? I mean what do the bits in square brackets mean? Where did the names ‘result’ and ‘value’ come from? How would one know the spec of the result returned by a particular ‘JsonImport’ invocation? Are there libraries of these things, or what? How much JavaScript will I have to know to use this kind of functionality?

Feeling stupid.

Graham



_______________________________________________
use-livecode mailing list
use-livecode at lists.runrev.com<mailto:use-livecode at lists.runrev.com>
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


More information about the use-livecode mailing list