FlorinSzasz Posted January 21, 2022 Share Posted January 21, 2022 Hi guys! I try to output a string from json but what i get is [ [ { "CapturePoints": 1100 } ] ] and [ [ { "CapturePoints": 600 } ] ] all i want is to remove the brackets if i use fromJSON i get something like this table: 070593C8 and table: 070593F0. Here is the code cappoints = function () local Query = dbQuery(db,"SELECT SUM(capturepoints) AS CapturePoints FROM grove") local Query2 = dbQuery(db,"SELECT SUM(capturepoints) AS CapturePoints FROM ballas") local rezultat = dbPoll(Query,-1) local rezultat2 = dbPoll(Query2,-1) local abc = toJSON(rezultat) local abc2 = toJSON(rezultat2) setElementData(source,"suma",abc) setElementData(source,"suma2",abc2) outputChatBox(""..string.gsub(abc,"[[{}]]","").."",source) outputChatBox(""..string.gsub(abc2,"[[{}]]","").."",source) end addEventHandler("onPlayerJoin",root,cappoints) Thank you in advance for anyone who can help me! Link to comment
Addlibs Posted January 21, 2022 Share Posted January 21, 2022 (edited) There is no built-in functions (perhaps other than inspect but that works recursively and returns a string with brackets) which does what you want; so, you must write your own. What you'll want in such function is to create a string, and concatenate/append to it each key and value pair that meets certain conditions (i.e. that it's a printable value, such as a string or number, not a table) separated by a colon and a space, and return the finalised string; then the caller function can print out the return. Edited January 21, 2022 by Addlibs Link to comment
The_GTA Posted January 21, 2022 Share Posted January 21, 2022 Hello FlorinSzasz, as brackets do you mean "{" (curly-brackets) or "[" (square brackets]? Link to comment
FlorinSzasz Posted January 22, 2022 Author Share Posted January 22, 2022 (edited) I try to get the value from the table or if i can remove the curly-brackets i am close but not there yet :)) cappoints = function () local Query = dbQuery(db,"SELECT SUM(capturepoints) AS CapturePoints FROM grove") local Query2 = dbQuery(db,"SELECT SUM(capturepoints) AS CapturePoints FROM ballas") local rezultat = dbPoll(Query,-1) local rezultat2 = dbPoll(Query2,-1) ------ for _, name in ipairs(rezultat) do local a = inspect(name) local string = string.gsub(a,"{","") local string2 = string.gsub(a,"}","") iprint(a) outputDebugString(""..string2.."") end end addEventHandler("onResourceStart",root,cappoints) i changed the code to debug faster. now i get "{\n CapturePoints = 1100\n}" from iprint and "{ CapturePoints = 1100 " from debug string output one more bracket left :)) Edited January 22, 2022 by FlorinSzasz Link to comment
Addlibs Posted January 22, 2022 Share Posted January 22, 2022 (edited) local function tableToString(table, bIgnoreKey) -- if bIgnoreKey is true, the function won't print the key name, useful only in list-like tables where -- the key is sequential and/or unused local str = "" for k, v in pairs(table) do if (type(k) == "string" or type(k) == "number") and (type(v) == "string" or type(v) == "number") then -- only add to string if both key and value are printable strings or numbers -- this ignores userdata, tables, elements, etc, so if you want them, you'll have to extend this -- function yourself if type(v) == "string" then -- add double quotation marks (" ... ") before and after a string value v = '"' .. v .. '"' end -- append key followed by colon and space if not ignored, then the value, a comma and space to the draft string str = str .. (bIgnoreKey and "" or (k .. ": ")) .. v .. ", " end end -- return the draft string with last two characters omitted (trailing comma and space) return str:sub(0, -2) end Does this function produce strings that you're looking for? Given you seem to want to print data out of a SQL query, I'm assuming you don't need functionality for printing userdata, elements, etc. only the standard printable types like string and number, right? local theTable = { CapturePoints = 1002 } outputChatBox(tableToString(theTable)) -- CapturePoints: 1002 local theTable = { CapturePoints = 1002, anotherKey = "someValue", ["key with spaces"] = 53.2 } outputChatBox(tableToString(theTable)) -- key with spaces: 53.2, anotherKey: "someValue", CapturePoints: 1002 -- note that the order is not guaranteed and can be different everytime the table changes and at different -- run times While this answers your question, it does seem a little over-engineered if all you're interested in is printing a single value out of the table, in which case it would be much simpler and faster to simply write if rezultat and rezultat.CapturePoints then outputChatBox("CapturePoints: " .. rezultat.CapturePoints) else outputChatBox("Failed to get CapturePoints") end Edited January 22, 2022 by Addlibs 1 Link to comment
FlorinSzasz Posted January 22, 2022 Author Share Posted January 22, 2022 i also looked up on wiki at that function didnt know how to modify it for my need. Thank you !! IT WORKS :D. I will use the string to make a 3D text with gang CapturePoints in ballas and grove gang base. In the past i did it but in a simple way and now i got back to scripting and i lost my last backup with scripts so i have to remade some of them and to fix this thing again . Thx Again!!! 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