<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之旅 廣告
                # 編碼為array還是object 首先大家請看這段源碼: ~~~ -- http://www.kyne.com.au/~mark/software/lua-cjson.php -- version: 2.1 devel local json = require("cjson") ngx.say("value --> ", json.encode({dogs={}})) ~~~ 輸出結果 > value --> {"dogs":{}} 注意看下encode后key的值類型,"{}" 代表key的值是個object,"[]" 則代表key的值是個數組。對于強類型語言(c/c++, java等),這時候就有點不爽。因為類型不是他期望的要做容錯。對于lua本身,是把數組和字典融合到一起了,所以他是無法區分空數組和空字典的。 參考openresty-cjson中額外貼出測試案例,我們就很容易找到思路了。 ~~~ -- 內容節選lua-cjson-2.1.0.2/tests/agentzh.t === TEST 1: empty tables as objects --- lua local cjson = require "cjson" print(cjson.encode({})) print(cjson.encode({dogs = {}})) --- out {} {"dogs":{}} === TEST 2: empty tables as arrays --- lua local cjson = require "cjson" cjson.encode_empty_table_as_object(false) print(cjson.encode({})) print(cjson.encode({dogs = {}})) --- out [] {"dogs":[]} ~~~ 綜合本章節提到的各種問題,我們可以封裝一個json encode的示例函數: ~~~ function json_encode( data, empty_table_as_object ) --lua的數據類型里面,array和dict是同一個東西。對應到json encode的時候,就會有不同的判斷 --對于linux,我們用的是cjson庫:A Lua table with only positive integer keys of type number will be encoded as a JSON array. All other tables will be encoded as a JSON object. --cjson對于空的table,就會被處理為object,也就是{} --dkjson默認對空table會處理為array,也就是[] --處理方法:對于cjson,使用encode_empty_table_as_object這個方法。文檔里面沒有,看源碼 --對于dkjson,需要設置meta信息。local a= {};a.s = {};a.b='中文';setmetatable(a.s, { __jsontype = 'object' });ngx.say(comm.json_encode(a)) local json_value = nil if json.encode_empty_table_as_object then json.encode_empty_table_as_object(empty_table_as_object or false) -- 空的table默認為array end if require("ffi").os ~= "Windows" then json.encode_sparse_array(true) end pcall(function (data) json_value = json.encode(data) end, data) return json_value 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>

                              哎呀哎呀视频在线观看