將Json字串轉成Lua Table 以及將Lua Table 轉成Json
4/27 更新
上一次寫的時候 沒有注意到陣列部分的轉換 所以這次做了一些修正 對於陣列轉換的部分加上去
function jsonToTable(str)
local result = {}
if tools.string.isEmpty(str) then
--print("jsonToTable str IsEmpty return default Table")
return result
end
local isArrayValue = false
local arrayValue = {}
local arrayValueIdx = 1
local arrayKey = ""
str = string.sub(str, 2, #str - 1)
local jsonSplit = string.split(str, ',')
for _, s in ipairs(jsonSplit) do
local keyValue = {}
local key = ""
local value = ""
if string.match(s, ':') ~= nil then
keyValue = string.split(s, ':')
key = string.sub(keyValue[1], 2, #keyValue[1] - 1)
value = keyValue[2]
else
value = s --setArrayValue
end
if isArrayValue == true then
_, endIdx = string.find(value, '%]')
if endIdx ~= nil then
local dataValue = convertJsonValue(string.sub(value, 1, endIdx - 1))
arrayValue[arrayValueIdx] = dataValue
result[arrayKey] = arrayValue
isArrayValue = false
arrayValue = {}
arrayValueIdx = 1
arrayKey = ""
else
arrayValue[arrayValueIdx] = convertJsonValue(value)
arrayValueIdx = arrayValueIdx + 1
end
goto continue
end
_, startIdx = string.find(value, '%[')
if startIdx ~= nil then
arrayKey = key
isArrayValue = true
local strValue = string.sub(value, startIdx + 1)
local dataValue = convertJsonValue(strValue)
arrayValue[arrayValueIdx] = dataValue
arrayValueIdx = arrayValueIdx + 1
goto continue
end
value = convertJsonValue(value)
result[key] = value
:: continue ::
end
return result
end
這裡加上了slua 迴圈 continue 的寫法
goto continue 這邊會直接跑到 :: continue :: 這裡 中間的程式碼都不會執行
把值轉換成table的地方 拆解出來成另一個function
function convertJsonValue(value)
if string.match(value, "false") ~= nil then
value = false
elseif string.match(value, "true") ~= nil then
value = true
elseif tonumber(value) ~= nil then
value = tonumber(value)
elseif string.match(value, "\"") ~= nil then
local startIdx = string.find(value, "\"")
local dataValue = string.sub(value, startIdx + 1, #value - 1)
if tonumber(dataValue) ~= nil then
value = tonumber(dataValue)
elseif tostring(dataValue) ~= nil then
value = tostring(dataValue)
end
else
print("jsonToTable Type is unKnow", type(value), "Value", value)
end
return value
end
將Table轉成Json字串格式
function serialize(obj)
local lua = ""
local t = type(obj)
if t == "number" then
lua = lua .. obj
elseif t == "boolean" then
lua = lua .. tostring(obj)
elseif t == "string" then
lua = lua .. string.format("%q", obj)
elseif t == "table" then
lua = lua .. "{\n"
lua = lua .. serializeTable(obj)
local metatable = getmetatable(obj)
if metatable ~= nil and type(metatable.__index) == "table" then
lua = lua .. serializeTable(metatable.__index)
end
lua = lua .. "}"
elseif t == "nil" then
return nil
else
error("can not serialize a " .. t .. " type.")
end
return lua
end
function serializeTable(table)
local runId = 1
local lua = ""
local tableCount = 0
for k, v in pairs(table) do
tableCount = tableCount + 1
end
for k, v in pairs(table) do
if runId < tableCount then
lua = lua .. serialize(k) .. ":" .. serialize(v) .. ",\n"
else
lua = lua .. serialize(k) .. ":" .. serialize(v) .. "\n"
end
runId = runId + 1
end
return lua
end