Erlkonig Posted April 25, 2021 Share Posted April 25, 2021 (edited) Hello everyone! Im trying to solve this problem already 3rd day but no results. I am calling HTTP request and it returns JSON. For example it works in browser (Firefox doing it better). When Im trying to call that request with a callRemote, I got returned the table but when I list that with a iteration it's clear as I understand. When I am doing that with fetchRemote, it returns the correct string but I need that the table ofc . I am sure that I am doing something wrong but I do not know what exactly I am doing wrong. Thanks for everyone who will reply. Edited April 25, 2021 by Erlkonig Link to comment
Moderators Patrick Posted April 25, 2021 Moderators Share Posted April 25, 2021 11 minutes ago, Erlkonig said: When I am doing that with fetchRemote, it returns the correct string but I need that the table ofc . Hi. You have to convert JSON to Lua array with fromJSON. (probably callRemote do it automatically) 1 Link to comment
Erlkonig Posted April 25, 2021 Author Share Posted April 25, 2021 28 minutes ago, Patrick said: Hi. You have to convert JSON to Lua array with fromJSON. (probably callRemote do it automatically) Hi! Thanks for reply! I tried but the table is empty while the string returns the information. The code below: function newResult(getupdates) if getupdates then outputConsole("Start of iteration") local jsonUpdates = fromJSON(getupdates) for i, v in ipairs(jsonUpdates) do outputConsole(i..": "..v) end outputConsole("End of iteration") outputConsole(getupdates) else outputDebugString("ERROR!", 3) end end fetchRemote(link, newResult) Link to comment
Moderators Patrick Posted April 25, 2021 Moderators Share Posted April 25, 2021 2 minutes ago, Erlkonig said: I tried but the table is empty while the string returns the information. How the string looks like? paste it here 1 Link to comment
Erlkonig Posted April 25, 2021 Author Share Posted April 25, 2021 Just now, Patrick said: How the string looks like? paste it here Is the the syntax which I used is correctly? Just a moment, I'll send the picture. The same HTTP request with firefox: Link to comment
Moderators Patrick Posted April 25, 2021 Moderators Share Posted April 25, 2021 Okay, so you have 2 fields in the json. ok and result function newResult(getupdates) if getupdates then outputConsole("Start of iteration") local jsonUpdates = fromJSON(getupdates) local ok = jsonUpdates["ok"] -- is 'ok' true? if ok then -- yes it was successful, so loop trough results local result = jsonUpdates["result"] -- you can print out tables to debug console to check content, with: iprint(result) for i, v in ipairs(result) do outputConsole(i..": update_id: "..v["update_id"].." | message_id: "..v["message"]["message_id"]) end end outputConsole("End of iteration") outputConsole(getupdates) else outputDebugString("ERROR!", 3) end end fetchRemote(link, newResult) Here you can read about tables: https://forum.multitheftauto.com/topic/130181-lua-tables-as-sets-instead-of-arrays/ 1 Link to comment
Erlkonig Posted April 25, 2021 Author Share Posted April 25, 2021 9 minutes ago, Patrick said: Okay, so you have 2 fields in the json. ok and result function newResult(getupdates) if getupdates then outputConsole("Start of iteration") local jsonUpdates = fromJSON(getupdates) local ok = jsonUpdates["ok"] -- is 'ok' is true? if ok then -- yes it was successful, so loop trough results local result = jsonUpdates["result"] -- you can print out tables to debug console to check content, with: iprint(result) for i, v in ipairs(result) do outputConsole(i..": update_id: "..v["update_id"].." | message_id: "..v["message"]["message_id"]) end end outputConsole("End of iteration") outputConsole(getupdates) else outputDebugString("ERROR!", 3) end end fetchRemote(link, newResult) Here you can read about tables: https://forum.multitheftauto.com/topic/130181-lua-tables-as-sets-instead-of-arrays/ Thank you very much! Also thanks for the topic with tutorial. It works already. I have to learn it! 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now