简体   繁体   中英

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

{
    "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM