1

我刚开始在我的项目中使用 luaj,我想在我的 lua 表中获取字符串内容。例如:

t = {
    subTitle = "Haircut",
}
return t;

我想得到字幕的内容,这应该很简单,但我不知道该怎么做。在我的代码中,我编写了如下示例代码:

public class MainActivity extends ActionBarActivity {

    Globals globals = JsePlatform.standardGlobals();
    TextView textView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R.id.text);

        try {
            LuaValue chunk = globals.loadfile("assets/Test.lua");
            String text = chunk.get("t").get("subTitle").call().tojstring();
            textView.append(text);
        }
        catch (Exception e)
        {
        }
    }

}

但它一直告诉我 get() 只能应用于 get 函数。如何获取 subTitle 的内容?非常感谢你的帮助。

4

3 回答 3

1

返回的块代表执行之前编译的脚本,因此您必须“调用()”一次该块才能执行它并获取结果。

也许这个例子更清楚:

Globals globals = JsePlatform.standardGlobals();
LuaValue chunk = globals.load(
    "t = {"+
    "    subTitle = 'Haircut',"+
    "};"+
    "return t");
LuaValue result = chunk.call();  // Executes the chunk and returns 't'
String text = result.get("subTitle").tojstring();
System.out.println(text);

您的脚本还对(全局)变量“t”进行了赋值,这在 globals 表中留下了副作用,因此您也可以从globals获取结果(在调用块之后) :

String text = globals.get("t").get("subTitle").tojstring();
System.out.println(text);
于 2015-07-21T13:41:37.950 回答
0

在 Java 中加载和串行遍历 lua 文件。

任意 lua 文件(test.lua):

return 
{
   myTable = 
   {
      { key1 = "a", ... },
      { key1 = "b", ... },
      { key1 = "c", ..., key5 = {key5.1="d", ...} }
   }
}

java是这样的(Android):

public static void loadLua(InputStream in)
{
    // Loading and serial traversal of a lua file in Java.
    //
    // create an environment to run in
    Globals globals = JsePlatform.standardGlobals();
    LuaValue chunk = globals.load(in, "@"+"test.lua", "bt");
    LuaClosure closure = new LuaClosure(chunk.checkclosure().p, globals); 

    //You must invokink ze closure to get ze data!
    LuaTable table = closure.call().checktable().get("myTable").checktable();

    //Serial traversal to an output stream.
    int l = table.length();
    for (int i =1;i<=l;i++)
    {
        System.out.println("---------------------------------------");
        System.out.println("key1:"+table.rawget(i).get("key1"));
        System.out.println("key2:"+table.rawget(i).get("key2"));
        System.out.println("key3:"+table.rawget(i).get("key3"));
        System.out.println("key4:"+table.rawget(i).get("key4"));

        // Key5 is a nested hash table
        LuaValue key5 = table.rawget(i).get("key5");
        if( !key5.isnil())
        {   
            System.out.println("Key5");
            LuaValue[] keys = key5.checktable().keys();
            for (int j = 0; j<keys.length; j++)
            {
                System.out.println("  ["+keys[j]+"]"+"key5.1:"+key5.checktable().get(keys[j]).get("key5.1"));
                System.out.println("  ["+keys[j]+"]"+"key5.2:"+key5.checktable().get(keys[j]).get("key5.2"));
                System.out.println("  ["+keys[j]+"]"+"key5.3:"+key5.checktable().get(keys[j]).get("key5.3"));
            }
        }
    }
}

编辑:如果有人想留下等效的 JSON 转换器,我会支持它。

于 2015-10-05T03:18:39.010 回答
0

Say we have this example piece of code written in lua :

t = { subTitle = "Haircut", }


you can do this in lua to parse only specific data

function getSubtitle() return t.subTitle end

and in java, to retrieve the data:

String v = globals.get("getSubtitle").invoke().toString();

于 2020-07-13T09:17:51.517 回答