<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # json解析的異常捕獲 首先來看最最普通的一個json解析的例子(被解析的json字符串是錯誤的,缺少一個雙引號): ~~~ -- http://www.kyne.com.au/~mark/software/lua-cjson.php -- version: 2.1 devel local json = require("cjson") local str = [[ {"key:"value"} ]] local t = json.decode(str) ngx.say(" --> ", type(t)) -- ... do the other things ngx.say("all fine") ~~~ 代碼執行錯誤日志如下: ~~~ 2015/06/27 00:01:42 [error] 2714#0: *25 lua entry thread aborted: runtime error: ...ork/git/github.com/lua-resty-memcached-server/t/test.lua:8: Expected colon but found invalid token at character 9 stack traceback: coroutine 0: [C]: in function 'decode' ...ork/git/github.com/lua-resty-memcached-server/t/test.lua:8: in function <...ork/git/github.com/lua-resty-memcached-server/t/test.lua:1>, client: 127.0.0.1, server: localhost, request: "GET /test HTTP/1.1", host: "127.0.0.1:8001" ~~~ 這可不是我們期望的,decode失敗,居然500錯誤直接退了。改良了一下我們的代碼: ~~~ local json = require("cjson") function json_decode(str) local data = nil _, err = pcall(function(str) return json.decode(str) end, str) return data, err end ~~~ 如果需要在Lua中處理錯誤,必須使用函數pcall(protected call)來包裝需要執行的代碼。pcall接收一個函數和要傳遞給后者的參數,并執行,執行結果:有錯誤、無錯誤;返回值true或者或false, errorinfo。pcall以一種"保護模式"來調用第一個參數,因此pcall可以捕獲函數執行中的任何錯誤。有興趣的同學,請更多了解下Lua中的異常處理。 另外,可以使用CJSON 2.1.0,該版本新增一個cjson.safe模塊接口,該接口兼容cjson模塊,并且在解析錯誤時不拋出異常,而是返回nil。 ~~~ local json = require("cjson.safe") local str = [[ {"key:"value"} ]] local t = json.decode(str) if t then ngx.say(" --> ", type(t)) end ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看