在lua中使用json库容易遇到的问题
来源:互联网 发布:俄语配音软件 编辑:程序博客网 时间:2024/06/05 19:05
现在有在lua里直接解析json的纯lua库,使得解析json很方便,尤其是在lua接入网络协议,读取本地配置尤为重要,但目前使用下来,存在一些问题。
使用的json库为网上的流行的开源json库,并且是cocos原生附带的json库,如下:
----------------------------------------------------------------------------- -- JSON4Lua: JSON encoding / decoding support for the Lua language. -- json Module. -- Author: Craig Mason-Jones -- Homepage: http://json.luaforge.net/ -- Version: 0.9.40 -- This module is released under the MIT License (MIT). -- Please see LICENCE.txt for details.
首先,lua本身不能识别\u83b7\u53d6\u6210\u529f这类unicode串,其次,lua也不支持解析”\/”这类转译,需要手动处理。
1.处理unicode的中文编码
--解析unicode编码的中文local bit = require("bit")local function unicode_to_utf8(convertStr) if base.type(convertStr)~="string" then return convertStr end local resultStr = "" local i = 1 while true do local unicode local num1 = string.byte(convertStr,i) if num1 ~= nil and string.sub(convertStr,i,i+1) == "\\u" then unicode = base.tonumber("0x"..string.sub(convertStr,i+2,i+5)) i= i+6 elseif num1~=nil then unicode = num1 i= i+1 else break end --base.print(unicode) if unicode <= 0x007f then resultStr = resultStr..string.char(bit.band(unicode,0x7f)) elseif unicode >= 0x0080 and unicode <= 0x07ff then resultStr = resultStr..string.char(bit.bor(0xc0,bit.band(bit.rshift(unicode,6),0x1f))) resultStr = resultStr..string.char(bit.bor(0x80,bit.band(unicode,0x3f))) elseif unicode >= 0x0800 and unicode <= 0xffff then resultStr = resultStr..string.char(bit.bor(0xe0,bit.band(bit.rshift(unicode,12),0x0f))) resultStr = resultStr..string.char(bit.bor(0x80,bit.band(bit.rshift(unicode,6),0x3f))) resultStr = resultStr..string.char(bit.bor(0x80,bit.band(unicode,0x3f))) end end --base.print(resultStr) return resultStr
end
local function utf8_to_unicode(convertStr) if base.type(convertStr)~="string" then return convertStr end local resultStr = "" local i = 1 local num1 = string.byte(convertStr,i) while num1 ~= nil do --base.print(num1) local tempVar1,tempVar2 if num1 >= 0x00 and num1 <= 0x7f then tempVar1 = num1 tempVar2 = 0 elseif bit.band(num1,0xe0)== 0xc0 then local t1 = 0 local t2 = 0 t1 = bit.band(num1,bit.rshift(0xff,3)) i = i+1 num1 = string.byte(convertStr,i) t2 = bit.band(num1,bit.rshift(0xff,2)) tempVar1 = bit.bor(t2,bit.lshift(bit.band(t1,bit.rshift(0xff,6)),6)) tempVar2 = bit.rshift(t1,2) elseif bit.band(num1,0xf0)== 0xe0 then local t1 = 0 local t2 = 0 local t3 = 0 t1 = bit.band(num1,bit.rshift(0xff,3)) i = i+1 num1=string.byte(convertStr,i) t2 = bit.band(num1,bit.rshift(0xff,2)) i = i+1 num1 = string.byte(convertStr,i) t3 = bit.band(num1,bit.rshift(0xff,2)) tempVar1=bit.bor(bit.lshift(bit.band(t2,bit.rshift(0xff,6)),6),t3) tempVar2=bit.bor(bit.lshift(t1,4),bit.rshift(t2,2)) end resultStr = resultStr..string.format("\\u%02x%02x",tempVar2,tempVar1) --base.print(resultStr) i = i+1 num1 = string.byte(convertStr,i) end --base.print(resultStr) return resultStrend
2.处理不能转译的\/字符
找到相应的串替换掉
0 0
- 在lua中使用json库容易遇到的问题
- ssh中使用json遇到的问题
- 在LUA与C交互中遇到的问题
- 在使用PowerDesigner中遇到的问题
- EXTJS在使用中遇到的问题
- 在使用DrawerLayout中遇到的问题
- js中使用JSON的时候遇到的问题
- Struts2中使用json遇到的一个小问题
- golang中使用package json遇到的问题
- golang json.Marshal使用中遇到的崩溃问题
- 标准库容器的使用
- cocos2d-x Lua使用pluginx中得IOSIAP遇到的问题
- 关于在使用(function(){})();中遇到的问题的记录
- 关于OPENCV中链接库容易加载错误的解决方案
- 在MIDAS中使用SocketConnection遇到的问题
- 在WIN32 DLL中使用MFC库遇到的问题
- 使用web service在实施中遇到的 问题
- 在PB中使用OCX控件遇到的问题
- 如何通curl登录淘宝这样的SSL站呢?
- java 向手机发送消息
- iOS quartzCore第四章——CAGradientLayer的使用
- 如何通curl登录淘宝这样的SSL站呢?
- Android 源码系列之<二>从安全的角度深入理解BroadcastReceiver(上)
- 在lua中使用json库容易遇到的问题
- UIKit性能调优实战讲解
- 淘宝api登陆,下载最近2天订单的php源代码
- 手机卫士 6
- Linux(2):Linux系统关闭与重启
- 细思极恐!带指纹解锁的手机更安全吗?
- 保证Linux Apache Web服务器安全的10个建议
- 常见Android App错误说明
- mysql的类dblink方案