add Dad Jokes sample
This commit is contained in:
24
README.md
24
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": "<https://icanhazdadjoke.com/j/7wciy59MJe|permalink> - <https://icanhazdadjoke.com|icanhazdadjoke.com>",
|
||||
"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
|
||||
|
||||
20
community/dad-jokes-widget.lua
Normal file
20
community/dad-jokes-widget.lua
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user