Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to extract this HTTP Data into a table?

Asked by 9 years ago
local http = game:GetService('HttpService')

local data = http:GetAsync('http://www.roproxy.tk/Asset/AvatarAccoutrements.ashx?userId=156')

That data gets all assets worn by builderman, but I need to find a way to get that data into a table, possibly through methods of string manipulation, but I'm not entirely sure how to do this.

2 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You could treat them as semicolon separated strings.

In that case, :gmatch is a good bet:

local list = {};
for url in data:gmatch("[^;]+") do
    -- Explanation of pattern:
        -- ; -- Semicolons
        -- [;] -- A "character class" consisting of only semicolons
        -- [^;] -- a class of all NOT semicolons
        -- [^;]+ -- one or more contiguous not-semicolons
    table.insert(list, url);
end
1
Wow, never knew about complement patterns. This is much better than my overcomplicated one. BlackJPI 2658 — 9y
Ad
Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

The only way I can think of how to do this is with a little bit of string manipulation. If you look the string GetAsync is returning, you'll notice that each asset is separated by a semi-colon [ ; ]. Using this, we can use some loops to find out how many assets there are (the last one does not have a semi-colon, so we'll need to add one) and then extract the asset ids from the string.

local http = game:GetService('HttpService')

local data = http:GetAsync('http://www.roproxy.tk/Asset/AvatarAccoutrements.ashx?userId=156')

function createTable()
    local array = {}
    local assetSeparators = {}

    -- Find number of assets
    for value in string.gmatch(data, ";") do
        table.insert(assetSeparators, value)
    end

    -- Extract assets
    local lastEnd = 1
    for i = 1, #assetSeparators do
        table.insert(array, string.sub(data, lastEnd, string.find(data, ";", lastEnd) - 1))
        lastEnd = string.find(data, ";", lastEnd + 1) + 1
    end
    table.insert(array, string.sub(data, lastEnd)) -- The asset without a semi-colon

    return array
end

local array = createTable()

Answer this question