Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
drinkrain
Level 7

Json Api help!

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!
0 Kudos
10 Replies
destruk
Level 10

Re: Json Api help!

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
0 Kudos
tim_beynart
Level 7

Re: Json Api help!

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.)
0 Kudos
tim_beynart
Level 7

Re: Json Api help!

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
0 Kudos
belltown
Level 7

Re: Json Api help!

"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/
0 Kudos
destruk
Level 10

Re: Json Api help!

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?
0 Kudos
belltown
Level 7

Re: Json Api help!

"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/
0 Kudos
destruk
Level 10

Re: Json Api help!

Thanks Belltown.
0 Kudos
tim_beynart
Level 7

Re: Json Api help!

Regarding JSON, check out this thread, I provided some example code: https://forums.roku.com/viewtopic.php?f=34&t=102328&sid=bb80464627580f0a1878995549bf7e31
0 Kudos
nmaves
Level 7

Re: Json Api help!

"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.
0 Kudos