zoukankan      html  css  js  c++  java
  • OpenResty json 删除转义符

    OpenResty 中删除 json 中的转义符

    cjson 在 encode 时  “/” 会自动添加转义符 “”; 在 decode 时也会自动将转义符去掉。工作中有个特殊需求,需要手工删除转义符。记录备忘,代码如下:

      1 #! /usr/bin/env lua
      2 json = require "cjson"
      3
      4 result = {}
      5 result["stream"] = "lufei"
      6 result["app"] = "live/cartoon"
      7
      8 oldStr = json.encode(result)
      9 local from, to, err = ngx.re.find(oldStr, [[\]])
     10 ngx.say(from, to)
     11 newStr, n, err = ngx.re.gsub(oldStr, [[\/]], [[/]])
     12 ngx.say("oldStr: "..oldStr)
     13 ngx.say("newStr: "..newStr )
     14
     16 t = json.decode(newStr)
     17 ngx.say(t["app"])
    dill@bunbun:~/openresty-test/locations$ curl -i localhost:6699/test
    HTTP/1.1 200 OK
    Server: openresty/1.11.2.2
    Date: Wed, 19 Jul 2017 04:38:07 GMT
    Content-Type: text/plain
    Transfer-Encoding: chunked
    Connection: keep-alive
    
    1313
    oldStr: {"app":"live/cartoon","stream":"lufei"}
    newStr: {"app":"live/cartoon","stream":"lufei"}
    live/cartoon
  • 相关阅读:
    UVA
    [CQOI2018] 社交网络
    UVA
    51nod 1314 定位系统
    51nod 1211 数独
    51nod 1392 装盒子
    51nod1253 Kundu and Tree
    51nod1313 完美串
    51nod1039 x^3 mod p
    51nod1369 无穷印章
  • 原文地址:https://www.cnblogs.com/DillGao/p/7205193.html
Copyright © 2011-2022 走看看