簡體   English   中英

如何在 LUA 中讀取 map

[英]How to read map in LUA

我是 LUA 的新手,我必須解析低於 JSON 的值,我需要閱讀屬性中定義的所有 val 和 attrid,屬性部分可能會有更多的值,我嘗試使用表格,但沒有運氣,任何幫助將不勝感激

{
    "obj1": {
        "attributes": [
            {
                "val": "1",
                "attrid": "test2"
            },
            {
                "val": "1",
                "attrid": "test1"
            }
        ]
        "status": 0
    }
}
-- Require some JSON library.
-- You can get lua-cjson from luarocks.
local json = require 'cjson'

-- You probably get this from a file or something in your actual code
local your_json_string = "string_containing_json"

-- Parse the json into a Lua table
local data = json.decode(your_json_string)

-- Iterate over the array like any other Lua sequence
for i, attribute in ipairs(data.obj1.attributes) do
   -- Do whatever you want with the val and attrid values
   print(attribute.val)
   print(attribute.attrid)
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM