Jump to content
  • 0

Encosing to Json


Topi

Question

2 answers to this question

Recommended Posts

You can even use this portable module made by @TisNquyen 

local json = {
 decode = function(js)
  local l = "return " .. js:gsub('("[^"]-"):','[%1]=')
  local tbl = load(l)()
  return tbl
 end,
 encode = function (tbl)
  return (dump or tostring)(tbl)
  :gsub("%['(%w+)'%] = ([%{'%w])", "\"%1\": %2")
  :gsub("%-%- table%b()", "")
  :gsub(": '(.-)'", ": \"%1\"")
  :gsub("([\"%w]),(%s*%})", "%1%2")
 end
}
-- // TEST // --
local js=[[
{
  "data": {
    "user": {
      "username": "username",
      "type": "TYPE"
    }
  },
  "passport": {
    "user": "uuid"
  },
}
]]
print("JSON : ", js)
local tbl = json.decode(js)
print("json.decode :", tbl)
print("json.encode :", json.encode(tbl))
--  Lua json for gg & andlua (simple)
-- join chat : @tisgroupchat
-- channel : @tisnquyen
-- final boss : @tis_nquyen

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.