lua解析json
2013-11-23 21:36
204 查看
自己写的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"));]]
--------------------------------------------------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"));]]
相关文章推荐
- luaError json解析错误1
- quick cocos2d-x lua解析json文件
- lua模块之cjson模块-json解析
- [寒江孤叶丶的Cocos2d-x之旅_16]Cocos2d-x LUA脚本中解析Json的方法
- Cocos2dx&Lua - UI显示优化之如何解决解析大量json文件
- lua解析json
- lua解析json
- cocos2dx lua json解析
- LUA解析json小demo
- Cocos2d-x教程(22)-lua中的json解析
- cocos2d-x lua 3.6 解析json
- Lua 使用cjson解析json数据(Mac环境)
- Lua json解析字段不存在处理方式
- Spring 注解的几种参数用途(@RequestParam @RequestBody)及JSON参数解析
- json生成与解析实例
- 分别讲解gson和fastjson解析json格式
- Android--解析JSON格式数据
- Hive数据仓库(11)之解析Json格式文件
- JS解析后台返回的JSON格式数据实例
- 动态读取JSON解析键值对的方法