jsonrokubrightscriptscenegraph

How do I Parse from Multiple JSON files in Roku Scenegraph (brightscript)?


I am using SceneGraph Developer Extensions SGDEX (Roku Brightscript). I have a pretty goodworking script set up. My current script parses from 1 JSON file. What I would like to do is parse from Multiple JSON files from a directory.

Currently I am parsing from 'http://feedserver.com/feed/feed.json' I woul like to parse from every file in the following directory 'http://feedserver.com/feed/'

I have tried simply changing the url to 'http://feedserver.com/feed' I have also tried using ListDir("http://feedserver.com/feed") in place of feed.= url.GetToSTring() In both cases, I get a blank screen when testing the script.

sub GetContent()
    url = CreateObject("roUrlTransfer")
    url.SetUrl("http://feedserver.com/feed.json")
    feed = url.GetToString()
    Sleep(2000) ' to emulate API call
    json = ParseJson(feed)
    rootNodeArray = ParseJsonToNodeArray(json)
    m.top.content.Update(rootNodeArray)
end sub

function ParseJsonToNodeArray(jsonAA as Object) as Object
    if jsonAA = invalid then return []
    resultNodeArray = {
       children: []
    }

    for each fieldInJsonAA in jsonAA
        ' Assigning fields that apply to both movies and series
        if fieldInJsonAA = "movies" or fieldInJsonAA = "series" or fieldInJsonAA = "search"
            mediaItemsArray = jsonAA[fieldInJsonAA]
            itemsNodeArray = []
            for each mediaItem in mediaItemsArray
                itemNode = ParseMediaItemToNode(mediaItem, fieldInJsonAA)
                itemsNodeArray.Push(itemNode)
            end for
            rowAA = {
               title: fieldInJsonAA
               children: itemsNodeArray
            }

           resultNodeArray.children.Push(rowAA)
       end if
    end for

    return resultNodeArray
end function

function ParseMediaItemToNode(mediaItem as Object, mediaType as String) as Object
    itemNode = Utils_AAToContentNode({
            "id": mediaItem.id
            "title": mediaItem.title
            "hdPosterUrl": mediaItem.thumbnail
            "Description": mediaItem.shortDescription
            "Categories": mediaItem.genres[0]
        })

    if mediaItem = invalid then
        return itemNode
    end if

    ' Assign movie specific fields
    if mediaType = "movies"
        Utils_forceSetFields(itemNode, {
            "Url": GetVideoUrl(mediaItem)
        })
    end if

    ' Assign series specific fields
    if mediaType = "series"
        seasons = mediaItem.seasons
        seasonArray = []
        for each season in seasons
            episodeArray = []
            episodes = season.Lookup("episodes")
            for each episode in episodes
                episodeNode = Utils_AAToContentNode(episode)
                Utils_forceSetFields(episodeNode, {
                    "url": GetVideoUrl(episode)
                    "title": episode.title
                    "hdPosterUrl": episode.thumbnail
                    "Description": episode.shortDescription
                })
                episodeArray.Push(episodeNode)
            end for
            seasonArray.Push(episodeArray)
        end for
        Utils_forceSetFields(itemNode, {
            "seasons": seasonArray
        })
    end if
    return itemNode
end function

function GetVideoUrl(mediaItem as Object) as String
    content = mediaItem.Lookup("content")
    if content = invalid then
        return ""
    end if

    videos = content.Lookup("videos")
    if videos = invalid then
        return ""
    end if

    entry = videos.GetEntry(0)
    if entry = invalid then
        return ""
    end if

    url = entry.Lookup("url")
    if url = invalid then
        return ""
    end if

    return url
end function

In the script example I provided it is a working script that parses 1 JSON file. I need the script to perform the exact same way, except parse multiple files in a directory.

Thanks,


Solution

  • I figured it out.

    Roku can produce the output from a php file, if it outputs json or xml. With that said, what I did was configure php from the server side, to pull from as many json feeds as I like, and point the urlfeed to the php file. See below.

    url = CreateObject("roUrlTransfer")
    url.SetUrl("http://feedserver.com/jsonfeed.php?output=json")