lua解析json

需要解析的json數據gui-config.json

{  
    "configs": [{  
        "server": "JP3.ISS.TF",  
        "server_port": 443,  
        "password": "58603228",  
        "method": "aes-256-cfb",  
        "remarks": ""  
    },  
    {  
        "server": "US1.ISS.TF",  
        "server_port": 443,  
        "password": "37382928",  
        "method": "aes-256-cfb",  
        "remarks": ""  
    },  
    {  
        "server": "HK2.ISS.TF",  
        "server_port": 8989,  
        "password": "59434206",  
        "method": "aes-256-cfb",  
        "remarks": ""  
    }],  
    "strategy": null,  
    "index": 0,  
    "global": false,  
    "enabled": true,  
    "shareOverLan": false,  
    "isDefault": false,  
    "localPort": 1080,  
    "pacUrl": null,  
    "useOnlinePac": false,  
    "availabilityStatistics": false  
}  

LUA解析代碼:

function FileRead()  
    local file = io.open("gui-config.json", "r");  
    local json = file:read("*a");  
    file:close();  
    return json;  
end  
  
function FileWrite()  
    local file = io.open("gui-config.json", "w");  
    file:close();  
end  
  
local cjson = require("cjson");  
local file = FileRead();  
local json = cjson.decode(file);  
for i, w in ipairs(json.configs) do  
    print("server: " .. w.password)  
    print("server_port: " .. w.server_port)  
    print("password: " .. w.password)  
    print("method: " .. w.method .. '\n')  
end  

輸出:

轉自:https://blog.csdn.net/linxinfa/article/details/76557700

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章