I have a string like this “{“Item”, “Item”}” and i want to turn it into a table like this {“Item”, “Item”}
Can i do that?
I have a string like this “{“Item”, “Item”}” and i want to turn it into a table like this {“Item”, “Item”}
Can i do that?
Why would i need to use HTTP service
Why would you not need to use HTTP service, is it not enough for you?
How do i use it to turn a string that LOOKS like a table into a table?
I sent it in my first reply, click
You sent the documentation, not wht function i need to use. Is it just json decode?
local HttpService = game:GetService("HttpService")
local jsonString = [[
{
"message": "success",
"info": {
"points": 120,
"isLeader": true,
"user": {
"id": 12345,
"name": "JohnDoe"
},
"past_scores": [50, 42, 95],
"best_friend": null
}
}
]]
local data = HttpService:JSONDecode(jsonString)
if data.message == "success" then
-- Since tab["hello"] and tab.hello are equivalent,
-- you could also use data["info"]["points"] here:
print("I have " .. data.info.points .. " points")
if data.info.isLeader then
print("I am the leader")
end
print("I have " .. #data.info.past_scores .. " past scores")
print("All the information:")
for key, value in pairs(data.info) do
print(key, typeof(value), value)
end
end
Just tells me “cant parse JSON”
Im using this:
local TestString = [[
{
"modclass": = "object",
"objectid": = "",
"objectname": = ""
}
]]
Idk anything about JSON
local HTTPService = game.HttpService
local TestString = [[
{
"objectid":"",
"modclass":"object",
"objectname":""
}
]]
local parsed = HTTPService:JSONDecode(TestString)
print(parsed)
Also why do you need this code for? Can you explain more about what you want to achieve? Why do you have a table as a string in the first place
have you tried roblox ai for help?
it should be pretty simple like doing operation on string
i.e.:
make a table variable- newTable={}
get each word
add it to the new table
i think it should be possible this way
Making a thingy that lets u add mods to the game, i decided to use tables as a thing to tell the game what to do, and i decided to use strings as an input, and i needed a table from that string
-- Your input string
local inputString = '{"Item1", "Item2"}'
-- Function to convert the string to a table
local function stringToTable(str)
-- Remove the curly braces
str = str:gsub("^%s*{(.-)}%s*$", "%1")
-- Split the string by ", "
local items = {}
for item in string.gmatch(str, '"(.-)"') do
table.insert(items, item)
end
return items
end
-- Call the function
local resultTable = stringToTable(inputString)
-- Print the results
for _, item in ipairs(resultTable) do
print(item) -- Output: Item1, Item2
end
HttpService:JSONDecode
JSONDecode
is likely faster overall since it’s optimized for parsing JSON.In short, if you’re working with simple strings, string operations might be quicker, but for anything more complicated, JSONDecode
is usually the better choice.
signed by (ChatGpt)
Idk, JSON decode is better in my opinion
Why does the speed matter if you won’t convert a trillion strings to tables
just a comparison between those two
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.