JSON API v2.0.1 for ComputerCraft
jonny190 09 Jul 2017
I'm trying to use obj.media_series_title but its outputting nothing, is there a way to use underscores?
Lignum 09 Jul 2017
jonny190, on 09 July 2017 - 11:03 PM, said:
I'm trying to use obj.media_series_title but its outputting nothing, is there a way to use underscores?
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"]
jonny190 10 Jul 2017
Dont Laugh too hard at my lack on coding ability
Edited by jonny190, 10 July 2017 - 06:39 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.
Lignum 10 Jul 2017
jonny190, on 10 July 2017 - 05:28 PM, said:
Dont Laugh too hard at my lack on coding ability
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.
jonny190 10 Jul 2017
good to know,
The Webserver outputs:
and the Monitor shows:
Edited by jonny190, 10 July 2017 - 07:13 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.
jonny190 10 Jul 2017
Sorted it, looks like i forgot about the initial attributes section, changed it to obj.attributes.media_series_title and everything works no problem
Can this API be used to send commands in JSON?
Can this API be used to send commands in JSON?
Lignum 10 Jul 2017
jonny190, on 10 July 2017 - 07:13 PM, said:
Can this API be used to send commands in JSON?
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.
jonny190 11 Jul 2017
Lignum, on 10 July 2017 - 10:04 PM, said:
jonny190, on 10 July 2017 - 07:13 PM, said:
Can this API be used to send commands in JSON?
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.
Lignum 11 Jul 2017
That's fairly straight forward. Simply make a table containing the request data, encode it, and then pass it as the POST body to http.post:
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()
CRUGG 31 May 2019
Hey there. I have a little Problem (it's probably something on my side), but could anyone help me?
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)
SquidDev 31 May 2019
CRUGG, on 31 May 2019 - 09:02 AM, said:
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)
CRUGG 31 May 2019
SquidDev, on 31 May 2019 - 09:14 AM, said:
CRUGG, on 31 May 2019 - 09:02 AM, said:
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)