自己写的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"));]]

lua解析json的更多相关文章

  1. LUA解析json小demo

    需要修改的json数据gui-config.json { "configs": [{ "server": "JP3.ISS.TF", &qu ...

  2. Xamarin.Android下获取与解析JSON

    一.新建项目 1.新建一个Android项目,并命名为为NetJsonList 2.右击引用,选择添加引用,引用System.Json.dll 二.同步请求 既然是跨平台,我们自然不能按照java下的 ...

  3. 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)

    在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...

  4. 阶段一:通过网络请求,获得并解析JSON数据(天气应用)

    “阶段一”是指我第一次系统地学习Android开发.这主要是对我的学习过程作个记录. 在上一篇阶段一:解析JSON中提到,最近在写一个很简单的天气预报应用.即使功能很简单,但我还是想把它做成一个相对完 ...

  5. 阶段一:解析JSON

    “阶段一”是指我第一次系统地学习Android开发.这主要是对我的学习过程作个记录. 最近学到解析JSON格式的网络数据,而作业也要求自己找一个天气预报的API地址,然后解析其中JSON格式的数据.可 ...

  6. C语言创建及解析Json的使用法则

    参考原文:http://blog.csdn.net/xukai871105/article/details/33013455 JSON(JavaScriptObject Notation)是一种轻量级 ...

  7. 不一样的dynamic解析json 万能方法

    写过javascript的人都知道js解析json 1:(JSON) 字符串转换为对象. var str = '{"name":"lsw","hobb ...

  8. C# 解析JSON的几种办法

    欲成为海洋大师,必知晓海中每一滴水的真名. 刚开始只是想找一个转换JSON数组的方法,结果在MSDN翻到一大把. 搜索过程中免不了碰到一大堆名词:WCF => DataContract => ...

  9. eval解析JSON中的注意点

       在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 使用eval函数来解析,并且使用jquery ...

随机推荐

  1. 使navicat可以通过SSH连接MySQL数据库

    1.编辑/etc/ssh/sshd_config,在最下面添加如下语句 KexAlgorithms diffie-hellman-group1-sha1,curve25519-sha256@libss ...

  2. perl学习笔记——输入与输出

    读取标准输入 用<STDIN>进行标准输入:chomp($line=<STDIN>); 如果读到文件尾,行输入操作符就会返回undef.便可利用这一性质跳出循环. while( ...

  3. 倍福TwinCAT(贝福Beckhoff)基础教程5.1 TwinCAT如何执行系统命令

    TwinCAT提供了一系列的执行Windows系统命令的方法 Name 描述 NT_Shutdown 关机操作系统 NT_AbortShutdown 取消关机操作系统命令 NT_Reboot 重启操作 ...

  4. badblocks检测磁盘坏道

    1.检测整个盘如/dev/sdb #badblocks -v /dev/sdb > badsectors.txt 2.检测一个分区#badblocks -v /dev/sdb1 > bad ...

  5. 网络编程readn、writen和readline函数的编写

    readn   在Linux中,read的声明为: ssize_t read(int fd, void *buf, size_t count); 它的返回值有以下情形: 1.大于0,代表成功读取的字节 ...

  6. Oracle11g登陆sqlplus时一直提示密码错误

    我安装oracle的时候有自己设置帐号和密码,我也在服务里的oracleserver中查看了正在运行的用户名,是我注册时填的那个并已开启.但是为什么登陆Sqlplus老是说密码错误呢?我敢肯定密码没有 ...

  7. sed `grep` 查找并替换

    sed "s/libletvwatermark/libletv_watermark/" `grep -rl libletvwatermark` grep [options] 3.主 ...

  8. Asp.net管道模型(管线模型)之一发不可收拾

    前言 为什么我会起这样的一个标题,其实我原本只想了解asp.net的管道模型而已,但在查看资料的时候遇到不明白的地方又横向地查阅了其他相关的资料,而收获比当初预想的大了很多. 有本篇作基础,下面两篇就 ...

  9. 苹果版小黄车(ofo)app主页菜单效果

    代码地址如下:http://www.demodashi.com/demo/12823.html 前言: 最近又是公司项目上线一段时间了,又是到了程序汪整理代码的节奏了.刚好也用到了ofo主页菜单的效果 ...

  10. 基于webmagic的种子网站爬取

    代码地址如下:http://www.demodashi.com/demo/12175.html 1. 概述 因为无聊,闲来没事做,故突发奇想,爬个种子,顺便学习爬虫.本文将介绍使用Spring/Myb ...