json - 将 json 文件与变量连接

标签 json lua coronasdk

我想将一个变量与名为 items.json 的文件中的表连接起来。我的文件内的属性称为“pos1_name”、“pos2_name”等...所以我想使用属性名称中的数字作为变量。我尝试过: item.pos..i.._name 但它不起作用。

 items = VarLoad.loadTable("items.json")

  for i=1,7  do
            --var ="1"
            --print(table.concat(items,"",2))
                if items.pos1=="1" then
                        item[i] = display.newImageRect("items/"..items.pos1_name..".png", 80 , 80)
                        item[i].x = holder[i].x+10
                        item[i].anchorX=0
                        item[i].y=holder[i].y
                        itemGroup:insert (item[i])
                        item[i].destination="item1"
                        item[i]:addEventListener( "touch", onSwitchPress )

                        item_bigsize[i] = display.newImageRect("items/"..items.pos1_name..".png", 240 , 240)
                        item_bigsize[i].x = 950
                        item_bigsize[i].anchorX=0
                        item_bigsize[i].y=display.contentCenterY-130
                        group:insert (item_bigsize[i])
                        item_bigsize[i].isVisible=true       


                end
        i=i+1
    end

最佳答案

My attributes inside of the files are called "pos1_name", "pos2_name", etc... So I would like to use the number in the attribute name, as a variable. I've try : item.pos..i.._name but it doesn't work.

您需要使用全表表示法:items.pos1_nameitems['pos1_name'] 的快捷方式,因此如果您需要动态生成 key ,您可以可以使用这样的东西:

print(items['pos'..i..'name'])

关于json - 将 json 文件与变量连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26427894/

相关文章:

javascript - 将 Lua 数据转换为 JSON

android - Corona SDK 允许设备音量控制

lua - io.popen() :lines() ignores first non-empty line

c# - Javascript json 错误 Invalid JSON primitive : strJson

javascript - 如何在 Angular JS 中使用 $Routeparams 从 json 文件检索数据

ios - -[NSURL _CFURLRequest] : unrecognized selector sent to instance

lua - Lua新手——表操作

sockets - 如何实时接收来自服务器的消息

ios - 如何在 Corona SDK 游戏应用程序中测量/量化 "sluggishness"?

java - 在 Java 中访问 JSON 嵌套值的动态方式