lua解析json

自己写的lua解析json,带容错,如果要是用于格式检查,得修改下。很简单直接贴代码

--------------------------------------------------json解析----------------------------------------------------------
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
        error(_format('json格式错误,不正确的数字, 错误位置:{from}', 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 == '\"' then
                return string.sub(str, from+1, i-1), i
            elseif char == '\\' then
                ignor = true
            end
        else
            ignor = false
        end
    end
    error(_format('json格式错误,字符串没有找到结尾, 错误位置:{from}', from))
end

local function json2array(str,from,to)
    local result = {}
    from = from or 1
    local pos = from+1
    local to = to or string.len(str)
    while(pos<=to) do
        local char = string.sub(str, pos, pos)
        if char == '\"' then
            result[#result+1], pos = json2string(str,pos,to)
--[[    elseif char == ' ' then
        
        elseif char == ':' then
            
        elseif char == ',' then]]
        elseif char == '[' then
            result[#result+1], pos = json2array(str,pos,to)
        elseif char == '{' then
            result[#result+1], pos = json2table(str,pos,to)
        elseif char == ']' then
            return result, pos
        elseif (char=='f' or char=='F') then
            result[#result+1], pos = json2false(str,pos,to)
        elseif (char=='t' or char=='T') then
            result[#result+1], pos = json2true(str,pos,to)
        elseif (char=='n') then
            result[#result+1], pos = json2null(str,pos,to)
        elseif (char=='N') then
            result[#result+1], pos = json2nan(str,pos,to)
        elseif numberchars[char] then
            result[#result+1], pos = json2number(str,pos,to)
        end
        pos = pos + 1
    end
    error(_format('json格式错误,表没有找到结尾, 错误位置:{from}', from))
end

function _G.json2table(str,from,to)
    local result = {}
    from = from or 1
    local pos = from+1
    local to = to or string.len(str)
    local key
    while(pos<=to) do
        local char = string.sub(str, pos, pos)
        if char == '\"' then
            if not key then
                key, pos = json2string(str,pos,to)
            else
                result[key], pos = json2string(str,pos,to)
                key = nil
            end
--[[    elseif char == ' ' then
        
        elseif char == ':' then
            
        elseif char == ',' then]]
        elseif char == '[' then
            if not key then
                key, pos = json2array(str,pos,to)
            else
                result[key], pos = json2array(str,pos,to)
                key = nil
            end
        elseif char == '{' then
            if not key then
                key, pos = json2table(str,pos,to)
            else
                result[key], pos = json2table(str,pos,to)
                key = nil
            end
        elseif char == '}' then
            return result, pos
        elseif (char=='f' or char=='F') then
            result[key], pos = json2false(str,pos,to)
            key = nil
        elseif (char=='t' or char=='T') then
            result[key], pos = json2true(str,pos,to)
            key = nil
        elseif (char=='n') then
            result[key], pos = json2null(str,pos,to)
            key = nil
        elseif (char=='N') then
            result[key], pos = json2nan(str,pos,to)
            key = nil
        elseif numberchars[char] then
            if not key then
                key, pos = json2number(str,pos,to)
            else
                result[key], pos = json2number(str,pos,to)
                key = nil
            end
        end
        pos = pos + 1
    end
    error(_format('json格式错误,表没有找到结尾, 错误位置:{from}', from))
end

--json格式中表示字符串不能使用单引号
local jsonfuncs={
    ['\"']=json2string,
    ['[']=json2array,
    ['{']=json2table,
    ['f']=json2false,
    ['F']=json2false,
    ['t']=json2true,
    ['T']=json2true,
}

function _G.json2lua(str)
    local char = string.sub(str, 1, 1)
    local func=jsonfuncs[char]
    if func then
        return func(str, 1, string.len(str))
    end
    if numberchars[char] then
        return json2number(str, 1, string.len(str))
    end
end

--[[
-----test-------
print("test json2lua");
dump(json2lua("false"));
dump(json2lua("true"));
dump(json2lua("{\"abc\":\"fge\"}"));
dump(json2lua("[\"afv\", 1, \"drrg\", false]"));
dump(json2lua("1234"));
dump(json2lua("null"));]]

转载于:https://www.cnblogs.com/marcher/p/json2lua.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值