简体   繁体   English

如何在 javascript 中使用来自 lua 文件的一些数据?

[英]How to use in javascript some data coming from lua files?

I have some lua files that I need to read in JS.我有一些需要在 JS 中读取的 lua 文件。 I would like to be able to import the variables (mostly tables) and go through them easily in JS, to add things in the DOM.我希望能够在 JS 中通过它们轻松导入变量(主要是表)和 go,以便在 DOM 中添加内容。

Exemple of LUA file: LUA 文件示例:

print("lua display text")

Civs = { 
    CIVILIZATION_EGYPT = { -- lua comment 1
        Playable = true,
        SpawnYear = -3000,
        StartX = 75,
        StartY = 32,
        Table = { },
        Text = "abc",
        TableTable = { {"a"}, {"b"}, {"c"}, {"d"}, {}, {}, {}},
    };
    CIVILIZATION_INDIA = { 
        Playable = true,
        SpawnYear = -3000,
        StartX = 98,
        StartY = 42,
        Table = { },
        Text = "abc",
        TableTable = { {"a"}, {"b"}, {"c"}, {"d"}, {}, {}, {}},
    };
    CIVILIZATION_BABYLON = {
        Playable = true,
        SpawnYear = -3000,
        StartX = 84,
        StartY = 41,
        Table = { },
        Text = "abc",
        TableTable = { {"a"}, {"b"}, {"c"}, {"d"}, {}, {}, {}},
     }
}
-- lua comment 2

There are multiple json libraries available for Lua.有多个 json 库可用于 Lua。 You can use them to serialize Lua tables into json strings.您可以使用它们将 Lua 表序列化为 json 字符串。

See http://lua-users.org/wiki/JsonModuleshttp://lua-users.org/wiki/JsonModules

Encoding example for dkjson: dkjson 的编码示例:

local json = require ("dkjson")

local str = json.encode (Civs, { indent = true })

print (str)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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