简体   繁体   English

如何在 LUA 中读取 map

[英]How to read map in LUA

I am new to LUA, I have to pars below JSON value, I need to read all the val and attrid define in attributes, there will be more value might come in the attributes section, I tried with table, but no luck, any help will be appreciated我是 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