Jump to content

Hi help me API respone fetchRemote


Recommended Posts

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
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 .. "]")

 

  • Like 1
Link to comment
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

  • Like 1
Link to comment
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
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.

 

  • Like 1
Link to comment
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

  • Like 1
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...