diff --git a/README.md b/README.md index 2f94545..08f8e6d 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,28 @@ function on_network_result(result) end ``` +Another example: + +``` +{ + "attachments": [ + { + "fallback": "What’s Forest Gump’s Facebook password? 1forest1", + "footer": " - ", + "text": "What’s Forest Gump’s Facebook password? 1forest1" + } + ], + "response_type": "in_channel", + "username": "icanhazdadjoke" +} +``` + +To extract "text" value we need to use this code: + +``` +joke = ajson:get_value(result, "object array:attachments object:0 string:text") +``` + Please note that the last element of the line should always be an instruction for extracting primitive data types: * `string:name` @@ -349,8 +371,6 @@ Please note that the last element of the line should always be an instruction fo * `double:name` * `boolean:name` -Also, instead of `object`, you can use `array` if the JSON contains an array. - To summarize: ajson works well (and very fast) when you need to retrieve one or two values. If you need to get a large amount of data (or all data) from JSON, then it is better to use the `json.lua` library (see below). It turns JSON into a set of easy-to-use nested Lua tables. ## Other diff --git a/community/dad-jokes-widget.lua b/community/dad-jokes-widget.lua new file mode 100644 index 0000000..2127b6c --- /dev/null +++ b/community/dad-jokes-widget.lua @@ -0,0 +1,20 @@ +-- name = "Dad jokes" +-- description = "icanhazdadjoke.com" +-- type = "widget" +-- author = "me" +-- version = "1.0" + +function on_resume() + http:get("http://icanhasdadjoke.com/slack") +end + +function on_network_result(result) + joke = ajson:get_value(result, "object array:attachments object:0 string:text") + ui:show_text(joke) +end + +function on_click() + if joke ~= nil then + system:copy_to_clipboard(joke) + end +end