Lua JSON 意外类型“function”

Lua JSON unexpected type `function`

提问人:Jan Intelkor 提问时间:8/24/2023 最后编辑:NifimJan Intelkor 更新时间:8/25/2023 访问量:46

问:

我有以下DCS:World脚本代码:

koUDPSocket = {}

koUDPSocket.host = "127.0.0.1"
--koTCPSocket.host = "85.221.224.254"
koUDPSocket.port = 52525
koUDPSocket.socket = socket.udp()
koUDPSocket.JSON = require "JSON"

function koUDPSocket:send(data, typee)
    local message = {
        typee = typee,
        data = data,
    }
    env.info("SENDING:")
    env.info(koEngine.TableSerialization(message))
    local jsonString = self.JSON:encode(message).." \n"

    socket.try(self.socket:sendto(jsonString, self.host, self.port))
    env.info("UDP DATA SENT")
 end
 


function countUnitsInTable(table)
    local count = 0
    for _, _ in pairs(table) do
        count = count + 1
    end
    return count
end

function koEngine.loadMissionDataShort()

    --debugee.pool()
    koEngine.debugText("koEngine.loadMissionDataShort")
    koEngine.debugText("loading shorted mission data")

    for categoryName, categoryTable in pairs(MissionData) do
        if type(categoryTable) == "table" and categoryName ~= "properties" then
            for objectiveName, objectiveTable in pairs(categoryTable) do
                if type(objectiveTable) == "table" then
                    local unitCount = 0
                    if objectiveTable.groups then
                        for _, group in pairs(objectiveTable.groups) do
                            if group.units then
                                unitCount = unitCount + countUnitsInTable(group.units)
                            end
                        end
                    end

                    local shortData = {
                        underAttack = objectiveTable.underAttack,
                        coa = objectiveTable.coa,
                        status = objectiveTable.status,
                        priority = objectiveTable.priority,
                        type = categoryName,
                        units = unitCount
                    }
                    missionDataShort[objectiveName] = shortData
                    koEngine.debugText("insert: " .. tostring(objectiveName))
                end
            end
        end
    end

    -- now send the data
    koUDPSocket:send(missionDataShort, "MissionDataShort")

    koEngine.debugText("END populating short data")
end

它从任务数据中获取所有目标,放入表中,并尝试发送到我的 Node.JS 服务器进行进一步处理。一切正常,期望给出错误“unecptected type:local jsonString = self.JSON:encode(message).." \n"function"

有趣的是,尝试会给 ,而不是type(message)tablefunction

我怎样才能确定真正的错误在哪里?我怎样才能调试数据以真正查看它是否是一个函数?最好的方法是什么?

下面是使用 env.info(koEngine.TableSerialization(message)) 和上一行输出的数据片段:

2023-08-23 19:27:44.840 INFO    SCRIPTING (Main): SENDING:
2023-08-23 19:27:44.848 INFO    SCRIPTING (Main): {
    ['data'] = {
        ['FARP Oche'] = {
            ['type'] = 'FARP',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 0,
            ['coa'] = 'neutral',
        },
        ['Antenna Bulls'] = {
            ['type'] = 'Communication',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 0,
            ['coa'] = 'neutral',
        },
        ['FARP Tsvirmi'] = {
            ['type'] = 'FARP',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 0,
            ['coa'] = 'neutral',
        },
        ['Sukhumi-Babushara'] = {
            ['type'] = 'Aerodrome',
            ['underAttack'] = false,
            ['status'] = 'open',
            ['priority'] = 'strategic',
            ['units'] = 27,
            ['coa'] = 'blue',
        },
        ['FARP Lentehi'] = {
            ['type'] = 'FARP',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 0,
            ['coa'] = 'neutral',
        },
        ['Antenna Balkariya'] = {
            ['type'] = 'Communication',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 3,
            ['coa'] = 'red',
        },
        ['Antenna Oche'] = {
            ['type'] = 'Communication',
            ['underAttack'] = false,
            ['status'] = 'closed',
            ['priority'] = 'strategic',
            ['units'] = 0,
            ['coa'] = 'neutral',
        },

我写了一个代码,测试了一个可能的问题,但事实并非如此,现在我想进一步调查

json lua-table

评论

0赞 ESkri 8/24/2023
你在哪里初始化变量?它看起来像一个已经包含某些内容(包括某处的函数)的全局变量 - 它是某个类的对象吗?missionDataShort

答:

0赞 Jan Intelkor 8/25/2023 #1

idk 我解决了这个问题,问题出在自己身上。JSON:编码。更改为 JSON.encode,并工作

评论

0赞 ESkri 8/25/2023
事实上,你应该写而不是self.JSON.encode(message)self.JSON:encode(message)