LUA, multiple number string to number

so I use web to request json:

{"number":"1,2,3"} OR table = {number="1,2,3"}

and when I used this, it shows number:

typeof(1,2,3)

but when i directly get the data from the json/table, it shows string, so is there anyway to convert it to show it as number?

1 answer

  • answered 2018-07-11 07:44 wp78de

    A Lua pattern might also a good choice to get the numbers from the raw string; then use tonumber() as suggested and add the numbers to a table in the sample code as shown below:

    numbers = {}
    str = '1,2,3'
    for num in string.gmatch(str, '([^,]+)') do
        table.insert(numbers, tonumber(num))
    end