如何在 brightscript 中使用变量获取 JSON 值
How to get JSON values using variable in brightscript
我正在尝试使用表示值的变量来解析此 JSON,但它不起作用。我不知道是因为变量不代表字符串,还是我不能做我想做的事情。
Function get_categoriesItems(category As Object) As Object
request = CreateObject("roUrlTransfer")
port = CreateObject("roMessagePort")
request.SetMessagePort(port)
request.SetUrl("http://jrocca.com/sample1.json")
if (request.AsyncGetToString())
while (true)
msg = wait(0, port)
if (type(msg) = "roUrlEvent")
code = msg.GetResponseCode()
if (code = 200)
playlist = CreateObject("roArray", 10, true)
print "";msg.GetString()
json = ParseJSON(msg.GetString())
print "";category
for each video in json.category
topic = {
Title: video.Title
Image: video.Image
Url: video.Url
}
playlist.push(topic)
print "";playlist
end for
return playlist
endif
else if (event = invalid)
request.AsyncCancel()
endif
end while
endif
return invalid
End Function
我该怎么做才能解决这个问题?
问题出在 for each video in json.category
行。将其替换为 for each video in json.lookup(category)
。这使用 roAssociativeArray.
的 Lookup(key as String) as Dynamic
函数
我正在尝试使用表示值的变量来解析此 JSON,但它不起作用。我不知道是因为变量不代表字符串,还是我不能做我想做的事情。
Function get_categoriesItems(category As Object) As Object
request = CreateObject("roUrlTransfer")
port = CreateObject("roMessagePort")
request.SetMessagePort(port)
request.SetUrl("http://jrocca.com/sample1.json")
if (request.AsyncGetToString())
while (true)
msg = wait(0, port)
if (type(msg) = "roUrlEvent")
code = msg.GetResponseCode()
if (code = 200)
playlist = CreateObject("roArray", 10, true)
print "";msg.GetString()
json = ParseJSON(msg.GetString())
print "";category
for each video in json.category
topic = {
Title: video.Title
Image: video.Image
Url: video.Url
}
playlist.push(topic)
print "";playlist
end for
return playlist
endif
else if (event = invalid)
request.AsyncCancel()
endif
end while
endif
return invalid
End Function
我该怎么做才能解决这个问题?
问题出在 for each video in json.category
行。将其替换为 for each video in json.lookup(category)
。这使用 roAssociativeArray.
Lookup(key as String) as Dynamic
函数