1

I just started using luaj in my project and I want to get the string content inside my lua table. For example:

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

I want get the content of subtitle which should be very simple but I have no idea how to do it. In my code, I wrote code like following example:

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)
        {
        }
    }

}

But it kept telling me get() can only be applied to get function. How can I get the content of subTitle? Thank you so much for helping.

Zelin Zhu
  • 11
  • 3

3 Answers3

1

The chunk returned represents the compiled script before executing it, so you have to "call()" the chunk once to execute it and get the result.

Perhaps this example is more clear:

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);

Your script also makes an assignment to the (global) variable "t" which leaves a side effect in the globals table, so you could also just get the result (after calling the chunk) from globals:

String text = globals.get("t").get("subTitle").tojstring();
System.out.println(text);
Jim Roseborough
  • 201
  • 1
  • 4
  • Thank you for your answer. But get() is the method to get function from Lua script. So after I tested your code, it didn't work. Fortunately I figured out the way to do it, simply by adding Lua function inside the Lua script, so I can call it to get string content through Java code whenever I want. – Zelin Zhu Jul 25 '15 at 20:40
0

Loading and serial traversal of a lua file in Java.

An arbitrary lua file (test.lua):

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

The java goes like this (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"));
            }
        }
    }
}

EDIT: If anyone would like to leave the equivalent jSON converter, I will upvote it.

Dominic Cerisano
  • 3,522
  • 1
  • 31
  • 44
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();