Your Digital Media Has Never Looked So Good

 
drinkrain
Topic Author
Posts: 10
Joined: Tue May 16, 2017 9:09 am

Json Api help!

Tue May 16, 2017 9:29 am

Hi,

I recently started building a Roku app. I noticed that there are a very limited amount of resources. This forum has been very helpful! I was wondering if you could give me some insight:

I am building an app using this repo: https://github.com/rokudev/hero-grid-channel as an example. The comments and print statements are very helpful. I am doing well with understanding the UI. But as far as bringing data in I am not sure where to start.

I have been provided a json api on the back end. 
I just need to find the best way to bring it in. The 'hero-grid' example seems to handle the data very well but they are using an RSS feed. Would it be naive to build something that consumes the api and 'recreates' the xml file? The example would be this link on the HeroScreen.brs file of that repo:
http://api.delvenetworks.com/rest/organ ... /media.rss

I feel like this might be the wrong approach. So Are there any repos or examples that are similar to hero-grid repo but consume an api? 

Any tips or advice will be VERY appreciated.

Thanks!
 
User avatar
destruk
Posts: 2503
Joined: Sat Dec 18, 2010 4:58 pm

Re: Json Api help!

Tue May 16, 2017 10:48 am

About a third of the utility/value of Roku is the ability to read content data from the internet.  It can read rss / xml / json / txt, and any other format that you can retrieve and program the app to make sense of.  When rebuilding the parsing routines, or modifying them to fit your content stream, speed is a key requirement.  I haven't looked at the "hero grid" code, but it is extremely popular.  If you put in a stop after the data is parsed, but before values are assigned, you can use the debugger to see what is there by printing the variable, and trying to get data out of the parsed array.  Ideally, for a content node, you would want to use setfields() as it is the fastest way to push data into a content node but to do that your source feed tags/names need to match what a content node uses.
Note that "Artist" is not currently included in the scenegraph content node, so you would need to use addfield to add it during runtime if you want to use that.
https://sdkdocs.roku.com/display/sdkdoc/ifSGNodeField

https://sdkdocs.roku.com/display/sdkdoc ... +Meta-Data
 
tim_beynart
Posts: 170
Joined: Wed Jul 15, 2015 8:30 am

Re: Json Api help!

Tue May 16, 2017 11:36 am

You want to read the data in whatever format it comes in, in your case JSON. Don't waste time (and resources) converting JSON to XML just so you can cut and paste the code from a demo.
To use JSON in brightscript, first you convert it to an BS associative array using parseJson()
For example, 

url = CreateObject("roUrlTransfer")
url.SetUrl("http://www.mycontentprovider.com/some_feed?format=json")
rsp = url.GetToString()
response_as_associative_array = parseJSON(rsp)


Like destruk said, you then loop through the nodes of your data and assign the values to the SG content node fields. Do you have a link to the hero grid example? (duh I can read.)
 
tim_beynart
Posts: 170
Joined: Wed Jul 15, 2015 8:30 am

Re: Json Api help!

Tue May 16, 2017 11:41 am

looking at the Hero Grid example, you would rewrite the parseResponse() in Parser.brs function to read your JSON data instead of XML.
https://github.com/rokudev/hero-grid-ch ... Parser.brs
 
belltown
Posts: 1445
Joined: Thu Dec 09, 2010 1:43 pm
Contact:

Re: Json Api help!

Tue May 16, 2017 1:04 pm

destruk wrote:
Note that "Artist" is not currently included in the scenegraph content node, so you would need to use addfield to add it during runtime if you want to use that.

You should be able to extend ContentNode to add whatever fields you want, and use your extended ContentNode as if it were an actual ContentNode:

<?xml version="1.0" encoding="UTF-8"?>
<component name="ContentItem" extends="ContentNode">
  <interface>
    <field id="HLS_URL" type="string" />
    <field id="MP3_URL" type="string" />
    <field id="artist" type="string" />
  </interface>
</component>
https://github.com/belltown/
 
User avatar
destruk
Posts: 2503
Joined: Sat Dec 18, 2010 4:58 pm

Re: Json Api help!

Tue May 16, 2017 5:41 pm

Good point Belltown -- if the firmware is altered to support Artist for a contentnode, would using the extended class cause a conflict or would adding the extra field this way simply be silently removed so the base class takes precedence?
 
belltown
Posts: 1445
Joined: Thu Dec 09, 2010 1:43 pm
Contact:

Re: Json Api help!

Tue May 16, 2017 7:19 pm

destruk wrote:
Good point Belltown -- if the firmware is altered to support Artist for a contentnode, would using the extended class cause a conflict or would adding the extra field this way simply be silently removed so the base class takes precedence?

According to https://sdkdocs.roku.com/display/sdkdoc/Creating+Custom+Components, under Characteristics of Components Extended from Custom Components:
<interface> fields accumulate: the extended component includes all of the interface fields of the component from which it is extended in addition to its own. In the case where an extended component field name is the same as the component from which it is extended, the definition of the extended component field is used, similar to functions.

It would seem to me that there would be no conflict; however, it's the extended component field name rather than the base class field name that takes precedence.
Note - I haven't tried this out yet. It would be an easy experiment to try overriding another existing field and see what happens.
https://github.com/belltown/
 
User avatar
destruk
Posts: 2503
Joined: Sat Dec 18, 2010 4:58 pm

Re: Json Api help!

Tue May 16, 2017 8:35 pm

Thanks Belltown.
 
tim_beynart
Posts: 170
Joined: Wed Jul 15, 2015 8:30 am

Re: Json Api help!

Wed May 17, 2017 1:47 pm

Regarding JSON, check out this thread, I provided some example code: https://forums.roku.com/viewtopic.php?f=34&t=102328&sid=bb80464627580f0a1878995549bf7e31
 
nmaves
Posts: 38
Joined: Mon Jul 10, 2017 6:52 pm

Re: Json Api help!

Thu Sep 21, 2017 9:29 am

destruk wrote:
About a third of the utility/value of Roku is the ability to read content data from the internet.  It can read rss / xml / json / txt, and any other format that you can retrieve and program the app to make sense of.  When rebuilding the parsing routines, or modifying them to fit your content stream, speed is a key requirement.  I haven't looked at the "hero grid" code, but it is extremely popular.  If you put in a stop after the data is parsed, but before values are assigned, you can use the debugger to see what is there by printing the variable, and trying to get data out of the parsed array.  Ideally, for a content node, you would want to use setfields() as it is the fastest way to push data into a content node but to do that your source feed tags/names need to match what a content node uses.
Note that "Artist" is not currently included in the scenegraph content node, so you would need to use addfield to add it during runtime if you want to use that.
https://sdkdocs.roku.com/display/sdkdoc/ifSGNodeField

https://sdkdocs.roku.com/display/sdkdoc ... +Meta-Data

Artist appears to be in the table of attributes on that second link yet I can't seem to use it.
 
User avatar
destruk
Posts: 2503
Joined: Sat Dec 18, 2010 4:58 pm

Re: Json Api help!

Thu Sep 21, 2017 10:49 am

Correct, it's a bug.  You can use a different field if available or add one.

Who is online

Users browsing this forum: CV27 and 3 guests