解决Lua脚本对网址解析去除"\"的方法

本文章为解决cjson对json数据添加自动添加转义字符问题

其他代码可以忽视主要看 红色 颜色标记的文字

不对url进行改进,选择在项目中改动可以工作量会大大提升,让脚本自身解决相关问题,达到代码可复用性

不添加获取的url:[{"name":"法外狂徒","url":"http:\/\/www.baidu.com"},{"name":"张三","url":"http:\/\/www.baidu.com\/news\/xxx.html"}]

添加后:[{"name":"张三","url":"http://www.baidu.com"},{"name":"xxx","url":"http://www.baidu.com/news/xxx.html"}]

local cjson = require("cjson");
local mysql = require("resty.mysql");
local db = mysql:new();
db:set_timeout(2000)
local props = {
host = "192.168.xxx.xxx",
port = 3306,
database = "changgou_content",
user = "root",
password = "root"
}
local res = db:connect(props);
local select_sql = "select name, url,from goods where status ='1' and goods_id="..id.." order by sort_order";
res = db:query(select_sql);

local responsejson = cjson.encode(res);

msg=ngx.re.gsub(responsejson,[[\\/]],[[/]]);

red:set("content_"..id,msg);
ngx.say(tostring(msg));


db:close()
else
cache_ngx:set('content_cache_'..id, rescontent, 10*60);
ngx.say(rescontent)
end

使用此方法可以解决url 对 "\"的转义 为后续调用做处理

希望对大家有所帮助

2020-10-11

posted @ 2020-10-11 13:21  就叫我蓝浅吧  阅读(723)  评论(1)    收藏  举报