C构造简单的lua表,以及嵌套表。
代码如下:
lua_newtable(L); //构造表
lua_pushnumber(L, i); //压入key
lua_pushlstring(L, str.c_str(),str.size()); //压入value
lua_settable(L, -3); //将key,value压入表,此时栈里面只有一个表,在栈顶。
lua_pushstring(L,fieldName.c_str()); //压入key
lua_newtable(L); //压入表
lua_settable(L, -3);//将ket,table压入表中
PS:我用lua_pushlstring是因为可能会压入非标准字符串,如果没有这个需求,那么可以用lua_pushstring代替,这个函数相对简单好理解。