我有以下代码:
PerformHttpRequest('http://ossie.dk/API/verify.json',function(err, text, headers)
local r=json.decode(text)
for s,t in pairs(r.servers)do
print(t)
end
end,"GET","",{})
我的问题是,当解码verify.json中的内容时,我一直收到这样的错误:错误的参数#1到"strfind"(应为字符串,得到nil(,或者尝试执行键的值不是表。在这一点上我真的很失落,花了这么多时间在这件事上。我认为整个问题在于JSON是如何设置的。它需要是一个字符串,但也需要一个表,我可以在以后用于键和值对,这样我就可以从中获得所有信息。我希望这能带来一些意义。
脚本错误来自这里:
local function scanwhite (str, pos)
while true do
pos = strfind (str, "%S", pos)
if not pos then return nil end
local sub2 = strsub (str, pos, pos + 1)
if sub2 == "239187" and strsub (str, pos + 2, pos + 2) == "191" then
-- UTF-8 Byte Order Mark
pos = pos + 3
elseif sub2 == "//" then
pos = strfind (str, "[nr]", pos + 2)
if not pos then return nil end
elseif sub2 == "/*" then
pos = strfind (str, "*/", pos + 2)
if not pos then return nil end
pos = pos + 2
else
return pos
end
end
end
我当前的JSON如下所示:
{
{ "servers":
"ip":
[
"144",
"155",
"166"
]
}
}
顺便说一句,我试过更换
r.servers
带有
r.ip
然后它给出:无效矢量字段:ip
如果我只是做
for k,v in pairs(r) do
它会给我这个错误:
Table expected, got string
我正在使用FiveM的JSON库。
如果你有一些问题或不理解我的意思,请在评论中提问。
您键入了str:find((和str:sub((命令。
local function scanwhite( str, pos )
while true do
pos = str :find( "%S", pos )
if not pos then return nil end
local sub1 = str :sub( pos, pos +1 )
local sub2 = str :sub( pos +2, pos +2 )
if sub1 == "239187" and sub2 == "191" then
-- UTF-8 Byte Order Mark
pos = pos +3
elseif sub1 == "//" then
pos = str :find( "[nr]", pos +2 )
if not pos then return nil end
elseif sub1 == "/*" then
pos = str :find( "*/", pos +2 )
if not pos then return nil end
pos = pos +2
else
return pos
end
end
end
第一个JSON有一个小问题。您使用的对象没有解密为您将要使用的JSON。请尝试此
{
{ "servers":
{
"ip": [
"144",
"155",
"166"
]
}
}
}