Forum Discussion

gabriel_parrale's avatar
9 years ago

Component Task to Consume Json

I make a task to consume a json
    m.readerTask = createObject("roSGNode", "ContentReader")
    m.readerTask.setField("uri", "https://.../metadata/x?limit=1")
    m.readerTask.observeField("content", "buildModel")
    m.readerTask.control = "RUN" 

I call the component that extends of task and I put the following:
  content = createObject("roSGNode", "ContentNode")
    readInternet = createObject("roUrlTransfer")
    readInternet.setUrl(m.top.uri)
    json = ParseJSON(readInternet.GetString())
             
I get the following error:
 
051:      readInternet = createObject("roUrlTransfer")
052:*     json = ParseJSON(readInternet.GetString())
Member function not found in BrightScript Component or interface. (runtime error &hf4) in pkg:/components/Content/conten
t.brs(52)
052:     json = ParseJSON(readInternet.GetString())

6 Replies

  • Thank you very much but now this:
    054:      json = ParseJSON(result)
    055:
    056:                      print json
    057:*                     for each kind in json.editorials
    ---------------------------------------------
    'Dot' Operator attempted with invalid BrightScript Component or interface refere
    nce. (runtime error &hec) in pkg:/components/Content/content.brs(57)
    057:                     for each kind in json.editorials


    This function probe with an interface example the basic navigation that does not use components, but in this new application that use components arise this problem, please can you help me[/size][/color]
  • By the error sounds as if `json` is `invalid`. What did the previous line show? Add maybe also a `print type(json)`
    056:                      print json
  • Invalid Return
    BRIGHTSCRIPT: ERROR: ParseJSON: Data is empty:

    In the basic navigation project if you brought me information, so I coded it and if it returns me:
    port = CreateObject("roMessagePort")
        request = CreateObject("roUrlTransfer")
        
        request.AddHeader("Authorization", "Basic <BASE 64 ENCODED SEGMENT WRITE KEY>")
        request.AddHeader("Accept", "application/json")
        request.AddHeader("Content-Type", "application/json")
        request.EnablePeerVerification(false)
        request.EnableHostVerification(false)
        request.RetainBodyOnError(true)
        request.SetCertificatesFile("common:/certs/ca-bundle.crt")
        request.InitClientCertificates()
        request.SetMessagePort(port)
        request.SetUrl("https://.../metadata/x?limit=1")
    if (request.AsyncGetToString())
            while (true)
                msg = wait(0, port)
                if (type(msg) = "roUrlEvent")
                    code = msg.GetResponseCode()
                    if (code = 200)
                        playlist = CreateObject("roArray", 10, true)
                        json = ParseJSON(msg.GetString())
                        'print json
                        for each kind in json.editorials
    -------------------------

    But when placing it in scene graph something I do wrong, taking into account that it is HTTPS I do the following:

      m.readerTask = createObject("roSGNode", "ContentReader")
        m.readerTask.setField("uri", "https://.../metadata/x?limit=1")
        m.readerTask.observeField("content", "buildModel")
        m.readerTask.control = "RUN" 

    I call the component that extends of task and I put the following:

     content = createObject("roSGNode", "ContentNode")
        contentxml = createObject("roXMLElement")
     
        readInternet = createObject("roUrlTransfer")
        readInternet.setUrl(m.top.uri)
        result = readInternet.getToString()
        json = ParseJSON(result)
                        for each kind in json.editorials

    There the error described appears to me, is there any other way to do it or that advises me to correct
  • Trace the problem back step by step!
    BRIGHTSCRIPT: ERROR: ParseJSON: Data is empty

    is telling you that `result` is empty string - or at least is not JSON. Print `result` to check, then follow back what could be the reason - was the URL wrong, did you forget the SSL certificates etc
        readInternet = createObject("roUrlTransfer")
        readInternet.setUrl(m.top.uri)
        result = readInternet.getToString()
        json = ParseJSON(result)
                        for each kind in json.editorials
  • Thank you very much, I followed your advice and giving different types of follow up and another few examples corrected the different errors, greetings