如何在LUA中解码json字符串?

时间:2018-01-19 13:33:22

标签: c# json lua

目前我正在使用lua,

集成的C#控制台应用程序

我的代码如下,

static void Main(string[] args)
    {
        Lua state = new Lua();

        var jsonConcate = @"[{ ""firstName"":""John"" , ""lastName"":""Doe"", ""email"":""NHP@123.COM"" },{ ""firstName"":""Anna"", ""lastName"":""Smith"", ""email"":""ASD@123.COM"" },{ ""firstName"":""Peter"" , ""lastName"":""Jones"", ""email"":""ZXC@123.COM""}]";

        state.DoString(@"
        function ScriptFunc (input)

            local json = require'json'

            local JSON_string = input

            return json.decode(JSON_string) end"
        );

        //            state.DoString(@"function ScriptFunc ( x, y )
        //  return x + y
        //end");

        //state.DoString(@"function concate(inputString) { var sample = JSON.parse(inputString); sample.forEach(function(a) { a.fullname = ['firstName', 'lastName'].map(function(k) { return a[k]; }).join(' ');});");
        var scriptFunc = state["ScriptFunc"] as LuaFunction;

        var res = scriptFunc.Call(jsonConcate);

        // LuaFunction.Call will also return a array of objects, since a Lua function
        // can return multiple values
    }

我在调用下面给出的函数时收到错误,

module 'json' not found:

no field package.preload['json']

我对此一无所知。

1 个答案:

答案 0 :(得分:0)

您需要在系统上安装json模块。 Lua默认没有这样的模块。