--这个原理是逐个解析字符串,同样可以解析json、xml
--这里解析json作为实例
luaJson = {
}
local function json2true(str, from, to)
return true, from + 3
end
local function json2false(str, from, to)
return false, from + 4
end
local function json2null(str, from, to)
return nil, from + 3
end
local function json2nan(str, from, to)
return nul, from + 2
end
local numberchars = {
['-'] = true,
['+'] = true,
['.'] = true,
['0'] = true,
['1'] = true,
['2'] = true,
['3'] = true,
['4'] = true,
['5'] = true,
['6'] = true,
['7'] = true,
['8'] = true,
['9'] = true,
}
local function json2number(str, from, to)
local i = from + 1
while (i <= to) do
local char = string.sub(str, i, i)
if not numberchars[char] then
break
end
i = i + 1
end
local num = tonumber(string.sub(str, from, i - 1))
if not num then
Log("red", 'json格式错误,不正确的数字, 错误位置:', from)
end
return num, i - 1
end
local function json2string(str, from, to)
local ignor = false
for i = from + 1, to do
local char = string.sub(str, i, i)
if not ignor then
if char == '\
Lua中的Json读取并转为table
最新推荐文章于 2023-02-03 18:14:03 发布