【问题标题】:Roku ParseJSON gives Unknow Identifier error when loading json via AJAX通过 AJAX 加载 json 时,Roku ParseJSON 给出未知标识符错误
【发布时间】:2020-04-25 00:52:39
【问题描述】:

我正在尝试编写一个简单的 Roku 应用程序。

当我通过 roURLTransfer 加载 JSON 文件时,ParseJSON 函数给了我BRIGHTSCRIPT: ERROR: ParseJSON: Unknown identifier

如果我通过 ReadAsciiFile("pkg:/feed/feed.json") 加载 JSON 文件,它可以工作。

JSON 文件是相同的,我很确定我的 JSON 是正确的。

url = "http://192.168.1.36/misc/roku/ifilm/feed.json"
result = ""
timeout = 10000

ut = CreateObject("roURLTransfer")
ut.SetPort(CreateObject("roMessagePort"))
ut.SetURL(url)
if ut.AsyncGetToString()
    event = wait(timeout, ut.GetPort())
    if type(event) = "roUrlEvent"
        result = event.GetString()
    elseif event = invalid
        ut.AsyncCancel()
    else
        print "roUrlTransfer::AsyncGetToString(): unknown event"
    end if
end if

' `print result` shows the correct lintable JSON
' print result
' Next line gives me: BRIGHTSCRIPT: ERROR: ParseJSON: Unknown identifier
json = ParseJSON(result)

但是将 JSON 文件放入应用程序中是可行的:

feed = ReadAsciiFile("pkg:/feed/feed.json")
sleep(2000)

json = ParseJson(feed)

我需要从 Internet 加载数据并且使用嵌入式版本对我没有帮助。有谁知道我应该怎么做才能让它工作?

【问题讨论】:

    标签: json roku brightscript


    【解决方案1】:

    “未知标识符” 错误通常是因为 json 字符串中有 ParseJson() 不支持的字符。 ReadAsciiFile() 起作用的原因可能是因为该函数通过应用 UTF-8 编码“清理”了 json 字符串。

    导致此问题的一些 JSON 响应开头出现的常见字符是 unicode 字符 Byte Order Mark (BOM)

    如果你用谷歌搜索“byte order mark json”,你会发现很多情况下这也会影响其他平台。

    在尝试解析字符串之前,您只需执行简单的查找和替换即可摆脱该字符。

    bomChar = Chr(65279)
    if result.left(len(bomChar)) = bomChar ' Check if the string has the BOM char prefix
        result = result.replace(bomChar, "")
    end if
    

    如果这不起作用,那么您的响应可能有一些其他冲突字符,在这种情况下,我建议使用 ifUrlTransfer::AsyncGetToFile() 而不是 AsyncGetToString() 然后使用 ReadAsciiFile() 这应该保证每个格式正确的 json 字符串时间(只要您的 json 有效)。

    【讨论】:

    • 感谢您的回答;这是 BOM。我的应用程序中的波斯字符显示为正方形时遇到问题,我心想也许使用 UTF-8 BOM 可能会解决问题,但没有运气!
    • 不!您看到的方块可能是因为您使用的字体。使用 Google 的 Noto 字体:google.com/get/noto
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    • 1970-01-01
    相关资源
    最近更新 更多