How to parse user data in Lua?

In Lua, you can utilize the JSON module from the standard library to parse user data. Firstly, make sure that the JSON module is already installed. It can be installed using the following command:

luarocks install json

Next, you can use the following sample code to parse user data:

local json = require('json')

-- 用户数据
local userData = [[
    {
        "name": "John",
        "age": 25,
        "email": "john@example.com",
        "address": {
            "street": "123 Main St",
            "city": "New York",
            "state": "NY"
        }
    }
]]

-- 解析用户数据
local data = json.decode(userData)

-- 输出解析结果
print("Name:", data.name)
print("Age:", data.age)
print("Email:", data.email)
print("Address:", data.address.street, data.address.city, data.address.state)

In the example above, the JSON module is first imported using require(‘json’). Then, the json.decode() function is used to parse user data and return a Lua table. Finally, the parsed data can be accessed by accessing the fields of the table.

The user data mentioned above is a JSON string, which can be replaced with the data provided by the user according to the actual situation.

bannerAds