Jump to content




JSON API v2.0.1 for ComputerCraft


54 replies to this topic

#41 jonny190

  • Members
  • 9 posts

Posted 09 July 2017 - 11:03 PM

I'm trying to use obj.media_series_title but its outputting nothing, is there a way to use underscores?

#42 Lignum

  • Members
  • 558 posts

Posted 09 July 2017 - 11:26 PM

 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"]


#43 jonny190

  • Members
  • 9 posts

Posted 10 July 2017 - 05:28 PM

Dont Laugh too hard at my lack on coding ability
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 Lignum

  • Members
  • 558 posts

Posted 10 July 2017 - 05:43 PM

 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.

#45 jonny190

  • Members
  • 9 posts

Posted 10 July 2017 - 06:16 PM

good to know,

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:
Posted Image

Edited by jonny190, 10 July 2017 - 07:13 PM.


#46 jonny190

  • Members
  • 9 posts

Posted 10 July 2017 - 07:13 PM

Sorted it, looks like i forgot about the initial attributes section, changed it to obj.attributes.media_series_title and everything works no problem :D

Can this API be used to send commands in JSON?

#47 Lignum

  • Members
  • 558 posts

Posted 10 July 2017 - 10:04 PM

 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.

#48 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 11 July 2017 - 12:36 AM

The alternate format is referred to as a data tag.

#49 jonny190

  • Members
  • 9 posts

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:

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.


#50 Lignum

  • Members
  • 558 posts

Posted 11 July 2017 - 10:28 PM

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()


#51 CRUGG

  • Members
  • 26 posts
  • Locationpropably in front of his PC

Posted 31 May 2019 - 09:02 AM

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)

#52 SquidDev

    Frickin' laser beams

  • Members
  • 1,403 posts
  • LocationDoes anyone put something serious here?

Posted 31 May 2019 - 09:14 AM

 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)
The top level object is an object, rather than an array - you'd need to do packageObj.luaide.name.

#53 CRUGG

  • Members
  • 26 posts
  • Locationpropably in front of his PC

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:

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)
The top level object is an object, rather than an array - you'd need to do packageObj.luaide.name.
Well, the problem with that is, that I want to go through all things with a for loop later. And the problem is, with an array I can't store any sub information. Is there any other way?

#54 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 01 June 2019 - 04:30 AM

Sounds like you're after a pairs loop.

#55 CRUGG

  • Members
  • 26 posts
  • Locationpropably in front of his PC

Posted 01 June 2019 - 04:04 PM

I don't really understand the link you sent me...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users