Installation of 1.lua 5.3
Go directly to the website to download
Http://www.lua.org/ftp/lua-5.3.3.tar.gz
Make Macosxsudo make install
2.CSJON
Compile the Lua-cjson library, with the Cloud Fork after the modified support Lua53 integer64 Library, Portal: Lua-cjson Library
Https://github.com/cloudwu/lua-cjson
Version: Lua Cjson 2.1
Changes to Makefile:
Lua_version = 5.3 #注意你的lua版本, you can use LUA-V to see the decimal point after one of my version is
Lua 5.3.1 Copyright (C) 1994-2013 lua.org, Puc-rio
PREFIX =/usr/local prefix path, installing LUA with make install, you can cd/usr/local to this path to see if there is a related library of Lua
Original: Cjson_ldflags =-shared
Change: cjson_ldflags =-bundle-undefined dynamic_lookup
Here is the difference between OSX and UNIX, the bundle is the file format used by Mac, which may cause "multiple LUA VMS detected" error if these options are not used
Add to:
Cjson_cflags + =-duse_internal_fpconv
Cjson_cflags + =-dieee_big_endian
Cjson_cflags + =-pthread-dmultiple_threads
Sample code
Parsing 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]);
Encoding JSON
Local Cjson = require"Cjson" local rettable = {}; --The resulting JSON table--sequential values local Intdatas = {};intdatas[1] =100;intdatas[2] ="100";--array local arydatas = {};arydatas[1] = {};arydatas[1]["Key 11"] ="Value one"; arydatas[1]["Key 12"] ="Value"; arydatas[2] = {};arydatas[2]["Key 21"] ="Value"; arydatas[2]["Key 22"] ="Value 22";--Table Assignment Value rettable["Key 1"] = "value 1"; Rettable[2] = 123;retTable[< Span class= "hljs-string" > "Int_datas"] = Intdatas;rettable[ "Arydatas"] = arydatas;-- Encode the table data into a JSON string local JSONSTR = Cjson.encode (rettable);p rint (JSONSTR);-the result is: {100, ""], "2": 123, "Key 1": "value 1", "Arydatas": [{ "key: " value ", "key All": "value One"},{ "key 21": "value", "key: " value "}]}
Install lua5.3 + Cjson under Mac