framef318 Posted October 29 Share Posted October 29 I tried using the API instead of direct mysql connection but I ran into a problem where I couldn't retrieve the API response value. It said it was a string. I tried converting it using toJSON and fromJSON and it didn't work. function hangetdleApiResponse(data, info) local json = toJSON(data) local result = fromJSON(data) if result then for _, user in ipairs(result) do // ipairs(json) iprint("ID: " .. user.id) iprint("Username: " .. user.username) iprint("Email: " .. user.email) end else iprint("Error parsing JSON data") end end function getApiData() local url = 'http://127.0.0.1:8000/user/' sendOptions = { connectionAttempts = 3, connectTimeout = 5000, method = "GET", formFields = {}, } fetchRemote(url, sendOptions, function(data, info) hangetdleApiResponse(data, info) end) end data repose json [ { "id": 49, "username": "framef318", "email": "[email protected]" }, { "id": 50, "username": "framef3188", "email": "[email protected]" } ] Link to comment
Moderators IIYAMA Posted October 29 Moderators Share Posted October 29 7 hours ago, framef318 said: local result = fromJSON(data) And if you do: local result = {fromJSON(data)} ? { "id": 49, "username": "framef318", "email": "[email protected]" }, This is one return value. local a, b = fromJSON(data) { "id": 50, "username": "framef3188", "email": "[email protected]" } And this is one return value. local a, b = fromJSON(data) Other way (untested): local result = fromJSON("[" .. data .. "]") 1 Link to comment
framef318 Posted October 30 Author Share Posted October 30 (edited) Thank you very much. I'll try it out. local a, b = fromJSON(data) If I can't know how many values to return, what do I do with local a, b? Edited October 30 by framef318 Link to comment
Nico834 Posted October 30 Share Posted October 30 2 hours ago, framef318 said: Thank you very much. I'll try it out. local a, b = fromJSON(data) If I can't know how many values to return, what do I do with local a, b? This function can return one table, which also contains other tables, so you don't need to declare multiple variables to store the return vale of it. The detailed description of the function can be found here: https://wiki.multitheftauto.com/wiki/FromJSON 1 Link to comment
framef318 Posted October 30 Author Share Posted October 30 12 minutes ago, Nico834 said: This function can return one table, which also contains other tables, so you don't need to declare multiple variables to store the return vale of it. The detailed description of the function can be found here: https://wiki.multitheftauto.com/wiki/FromJSON I still don't understand, does that mean I can use for in and fromJSON(data)? But it seems like I've done this before and it didn't produce any results. Link to comment
Moderators IIYAMA Posted October 30 Moderators Share Posted October 30 14 hours ago, framef318 said: If I can't know how many values to return, what do I do with local a, b? That is why you can capture those by surrounding the fromJSON with a table: local result = {fromJSON(data)} Although this solution might be more optimised: local result = fromJSON("[" .. data .. "]") It changes your data string to: [[ { "id": 49, "username": "framef318", "email": "[email protected]" }, { "id": 50, "username": "framef3188", "email": "[email protected]" } ]] Which helps returning everything inside of 1 variable, instead of multiple. 1 Link to comment
framef318 Posted October 31 Author Share Posted October 31 10 hours ago, IIYAMA said: That is why you can capture those by surrounding the fromJSON with a table: local result = {fromJSON(data)} Although this solution might be more optimised: local result = fromJSON("[" .. data .. "]") It changes your data string to: [[ { "id": 49, "username": "framef318", "email": "[email protected]" }, { "id": 50, "username": "framef3188", "email": "[email protected]" } ]] Which helps returning everything inside of 1 variable, instead of multiple. Thank you 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