JSON API v2.0.1 for ComputerCraft
#41
Posted 09 July 2017 - 11:03 PM
#42
Posted 09 July 2017 - 11:26 PM
jonny190, on 09 July 2017 - 11:03 PM, said:
That should work, so the problem is most likely somewhere else. Post the rest of the relevant code, please.
Either way, an idiotproof method that works with any string is to use [] to index obj:
obj["media_series_title"]
#43
Posted 10 July 2017 - 05:28 PM
while true do monitor = peripheral.wrap("back") os.loadAPI("json") str = http.get("https://home.daveys.xyz/api/states/media_player.firetv").readAll() obj = json.decode(str) monitor.clear() monitor.setCursorPos(1,2) monitor.write("Kodi is = ") monitor.write(obj.state) monitor.setCursorPos(1,4) monitor.write("Kodi is playing", obj.media_series_title) os.sleep( 3 ) term.clear() monitor.clear() monitor.setCursorPos(1,2) sleep(0.05) end
Edited by jonny190, 10 July 2017 - 06:39 PM.
#44
Posted 10 July 2017 - 05:43 PM
jonny190, on 10 July 2017 - 05:28 PM, said:
Oh, don't worry, there's significantly worse to be found around here.
That code looks fine to me, though. Does the monitor.write(obj.state) bit work? This is odd because if media_series_title wasn't indexed properly, it would write "nil" on the screen, not nothing, unless it's out of bounds for some reason. Are you sure the server is returning correct json? You might want to put print(str) after you http.get it to ensure that it's correct.
#45
Posted 10 July 2017 - 06:16 PM
The Webserver outputs:
"attributes": {"entity_picture": "/api/media_player_proxy/media_player.firetv?token=17f0d625441c059b681a532151a980af4dec61f14ccccaca413a9ec59646168d&cache=c61e3", "friendly_name": "FireTV", "is_volume_muted": false, "media_album_name": "", "media_content_id": {"unknown": ""}, "media_content_type": "tvshow", "media_duration": 1270, "media_episode": 15, "media_season": 1, "media_series_title": "American Dad!", "media_title": "American Dad S01E15", "supported_features": 55103, "volume_level": 1.0}, "entity_id": "media_player.firetv", "last_changed": "2017-07-10T18:42:20.419488+00:00", "last_updated": "2017-07-10T18:42:20.419488+00:00", "state": "playing"}
and the Monitor shows:
Edited by jonny190, 10 July 2017 - 07:13 PM.
#46
Posted 10 July 2017 - 07:13 PM
Can this API be used to send commands in JSON?
#47
Posted 10 July 2017 - 10:04 PM
jonny190, on 10 July 2017 - 07:13 PM, said:
I'm not sure about what exactly you mean, but you can use json.encode to turn any table into a JSON-encoded string. Though, if you mean a literal Minecraft server command, you should use textutils.serialiseJSON, since the format isn't actually entirely JSON, it's slightly different.
#49
Posted 11 July 2017 - 10:16 PM
Lignum, on 10 July 2017 - 10:04 PM, said:
jonny190, on 10 July 2017 - 07:13 PM, said:
I'm not sure about what exactly you mean, but you can use json.encode to turn any table into a JSON-encoded string. Though, if you mean a literal Minecraft server command, you should use textutils.serialiseJSON, since the format isn't actually entirely JSON, it's slightly different.
Im trying to use RESTful API (https://home-assista...opers/rest_api/) for home assistant to make a minecraft button turn on a light in real life
So i'll be trying to call http://192.168.5.149.../switch/turn_on
{"entity_id": "switch.christmas_lights"}
Thanks,
Jonny
Edited by jonny190, 11 July 2017 - 10:30 PM.
#50
Posted 11 July 2017 - 10:28 PM
local request = { entity_id = "switch.christmas_lights" } local response = http.post("http://192.168.5.149:8123/api/services/switch/turn_on", json.encode(request)) response.close()
#51
Posted 31 May 2019 - 09:02 AM
packageUrl = http.get("https://ploxion.github.io/plx-ppm-packages/packages.json").readAll() packageObj = json.decode(packageUrl) value = packageObj[0].name print(value)This code doesn't work. What this should do is to print the name of the first Element in the JSON (https://ploxion.gith...s/packages.json)
#52
Posted 31 May 2019 - 09:14 AM
CRUGG, on 31 May 2019 - 09:02 AM, said:
#53
Posted 31 May 2019 - 09:16 AM
SquidDev, on 31 May 2019 - 09:14 AM, said:
CRUGG, on 31 May 2019 - 09:02 AM, said:
#55
Posted 01 June 2019 - 04:04 PM
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users