With the 4.8 software release for Roku 2, Roku LT and Roku HD players, developers now have access to native JSON parsing support from BrightScript. Roku channel developers no longer need to integrate third party solutions or worse, roll their own parsing solutions anymore. Developers can easily consume JSON based feeds or REST API response payloads with one simple API call.
To parse JSON from BrightScript, simply call the new ParseJSON API:
ParseJSON(jsonString As String) As Object
ParseJSON takes a single string argument that contains the JSON content that you want to parse. This can come from any source, such as a text file installed as part of your channel or the response from an HTTP request executed with an roUrlTransfer object. If the string contains valid, well-formed JSON, the object returned is an associative array representation of the parsed JSON.
For example, let’s assume that the string jsonAsString contains the following JSON content:
{
"Videos" : [
{
"Title" : "Jeff Han Demos his Breakthrough Touchscreen",
"Image" : "http://rokudev.roku.com/rokudev/examples/videoplayer/images/JeffHan.jpg",
"Url" : "http://video.ted.com/talks/podcast/JeffHan_2006_480.mp4"
},
{
"Title" : "David Kelley on Human-Centered Design",
"Image" : "http://rokudev.roku.com/rokudev/examples/videoplayer/images/DavidKelley.jpg",
"Url" : "http://video.ted.com/talks/podcast/DavidKelley_2002_480.mp4"
},
…
]
}
A call to ParseJSON would then return an object that can be used like this:
json = ParseJSON(jsonAsString) for each video in json.Videos print video.Title; video.Image; video.Url end for
To help get you started using Roku’s new JSON support, I’ve built a short sample application that demonstrates how to parse a JSON text file which populates the content in a simple poster screen style UI. This application also provides an XML driven version of the same functionality so that you can compare JSON parsing to XML parsing in one convenient sample. The sample can be found here:
Download BrightScript JSON Parsing Sample
More details on the new JSON API can be found in the Roku SDK documentation:
More on the JSON API
Pingback: Communicating with Web Services from BrightScript | Developer Blog