mac 下安裝 lua5.3 + cjson

來源:互聯網
上載者:User

標籤:

1.lua 5.3的安裝 

直接去官網下載 

http://www.lua.org/ftp/lua-5.3.3.tar.gz

make macosxsudo make install

2.CSJON 

編譯lua-cjson庫,用的是雲風fork後修改的支援lua53 integer64的庫,傳送門:lua-cjson庫

https://github.com/cloudwu/lua-cjson

 

版本:Lua CJSON 2.1 

Makefile的改動:

LUA_VERSION =       5.3  #注意你的lua版本,可以用lua -v看 取小數點後一位 我的版本是

Lua 5.3.1  Copyright (C) 1994-2013 Lua.org, PUC-Rio

PREFIX =            /usr/local  首碼路徑,安裝lua的時候用 make install 安裝;可以cd /usr/local到該路徑查看是否存在lua的相關庫

原:CJSON_LDFLAGS =     -shared

改:CJSON_LDFLAGS =     -bundle -undefined dynamic_lookup

 

這裡就是OSX和Unix的區別,bundle是Mac使用的檔案格式,如果不使用這些選項,可能引起“multiple lua vms detected”錯誤

 

添加:


CJSON_CFLAGS +=     -DUSE_INTERNAL_FPCONV

CJSON_CFLAGS +=     -DIEEE_BIG_ENDIAN

CJSON_CFLAGS +=     -pthread -DMULTIPLE_THREADS 

範例程式碼

解析JSON

local cjson = require "cjson"local sampleJson = [[{"age":"23","testArray":{"array":[8,9,11,14,25]},"Himi":"himigame.com"}]];--解析json字串local data = cjson.decode(sampleJson);--列印json字串中的age欄位print(data["age"]);--列印數組中的第一個值(lua預設是從0開始計數)print(data["testArray"]["array"][1]);   

編碼JSON

local cjson = require "cjson"local retTable = {};    --最終產生json的表--順序數值local intDatas = {};intDatas[1] = 100;intDatas[2] = "100";--數組local aryDatas = {};aryDatas[1] = {};aryDatas[1]["鍵11"] = "值11";aryDatas[1]["鍵12"] = "值12";aryDatas[2] = {};aryDatas[2]["鍵21"] = "值21";aryDatas[2]["鍵22"] = "值22";--對Table賦值retTable["鍵1"] = "值1";retTable[2] = 123;retTable["int_datas"] = intDatas;retTable["aryDatas"] = aryDatas;--將表資料編碼成json字串local jsonStr = cjson.encode(retTable);print(jsonStr);--結果是:{"int_datas":[100,"100"],"2":123,"鍵1":"值1","aryDatas":[{"鍵12":"值12","鍵11":"值11"},{"鍵21":"值21","鍵22":"值22"}]}

mac 下安裝 lua5.3 + cjson

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.