Question

I have a 2 part question that I am hoping someone can help me with.

I am trying to figure out how to pass an array of years from my server to my ROKU code. It is going to be a variable number of years, meaning there won't always be the same number of elements in the array.

What I need to figure out is how to set up the array, and then how to parse it in BrightScript.

Here is an example of what I have so far. I have the count, but still need to add all the years.

The list of years is 1998,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014

{
"Items" : [
  {
    "name": "count",
    "value": "13"
  }
]}

Once it's received in my ROKU code, I will need to loop over the years, and display them, one per line. (I already have the display part done. I just need help knowing how to get at the array elements.)

Since there is so little useful documentation out there for BrightScript arrays, I'm hoping someone with more experience will be able to answer this, and that it will help someone else in the future.

Was it helpful?

Solution

You don't need to do any of that. Just send the JSON array and Roku will parse it:

BrightScript Debugger> myJSON = "[1998,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014]" 
BrightScript Debugger> ? type(myJSON)
String

BrightScript Debugger> arr = parseJSON(myJSON)
BrightScript Debugger> ? type(arr)
roArray

It's perfectly valid to have array as top-level element of the json text ("A JSON text is a serialized object or array." per RFC 4627). You can also do it with a dummy object (but why?): arr = parseJSON(" {"years": [1998, 2003]} ").years

OTHER TIPS

Here is how I solved it:

Array:

{ 
 "Items" : [ 
     { "value": "2014" } , 
     { "value": "2013" } , 
     { "value": "2012" } , 
     { "value": "2011" } , 
     { "value": "2010" } , 
     { "value": "2009" } , 
     { "value": "2008" } , 
     { "value": "2007" } , 
     { "value": "2006" } , 
     { "value": "2005" } , 
     { "value": "2004" } , 
     { "value": "2003" } , 
     { "value": "1998" } 
   ]
} 

BrightScript code to parse it:

    arr = CreateObject("roArray",json.Items.count(),false)

    for each item in json.Items
       thisItem = {
        value: item.value         
       }
       arr.push(thisItem)
    end for
    return arr

I hope this helps someone else in the future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top