1

I have the following code:

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","",{})
               

My problem is, when decoding the things from the verify.json i keep receiving either this error: bad argument #1 to 'strfind' (string expected, got nil) or that what trying to do a key, value for is not a table. Im really lost at this point, spend so much time on this thing. I think the whole problem is how the JSON is set up. It needs to be a string, but also a table that I can use for my key, value pairs later on so I can get all the info from it. I hope this makes some sort of sence.

The scripting error is coming from here:

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 == "\239\187" and strsub (str, pos + 2, pos + 2) == "\191" then
      -- UTF-8 Byte Order Mark
      pos = pos + 3
    elseif sub2 == "//" then
      pos = strfind (str, "[\n\r]", 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

My current JSON looks like this:

{   
    { "servers": 
        "ip": 
        [
            "144",
            "155",
            "166"
        ]
    }
}

BTW I have tried changing

r.servers

with

r.ip

then it gives this: invalid vector field: ip

If I just do

for k,v in pairs(r) do

It will give me this error:

Table expected, got string

Im using the JSON library from FiveM.

If you have some questions or dont understand what I mean feel free to ask in the comments.

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
OssieFromDK
  • 51
  • 2
  • 6

2 Answers2

0

you typo'd your str:find() and str:sub() commands.

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 == "\239\187" and sub2 == "\191" then
      -- UTF-8 Byte Order Mark
      pos = pos +3
    elseif sub1 == "//" then
      pos = str :find( "[\n\r]",  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
Doyousketch2
  • 2,060
  • 1
  • 11
  • 11
0

The first JSON has a little problem. You use an object without declarin to JSON that you will use it. Try this

{   
    { "servers":
        { 
            "ip": [
                "144",
                "155",
                "166"
            ]
        }
    }
}