如何在Lua中将双引号"\""输出到控制台?

我正在尝试将Lua中的一个连接字符串输出到控制台。当该字符串在控制台中显示时,自动在其前后添加一个双引号。我想在字符串中间有一些其他双引号,但我不能这样做。

在下面的注释中,我尝试了几种不同的方法,但是没有一种起作用。输出通常是这样的:

1) "10000\": \"1543412332"
2) "10001\": \"1543233731"
3) "10003\": \"1543637245"
4) "10004\": \"1543227124"
5) "10005\": \"1543226828"

但我希望输出为:

1) "10000": "1543412332"
2) "10001": "1543233731"
3) "10003": "1543637245"
4) "10004": "1543227124"
5) "10005": "1543226828"

下面是我的代码

    for index = 1, table.maxn(resultKey) do
       local unconcatted = {[1] = resultKey[index], [2] = [[": "]], [3] = resultValue[index]}
    -- local unconcatted = {[1] = "\"", [2] = resultKey[index], [3] = "\": \"", [4] = resultValue[index], [5] = "\""}
    -- local unconcatted = {[1] = resultKey[index], [2] = "\": \"", [3] = resultValue[index]}
    -- local unconcatted = {[1] = resultKey[index], [2] = '\": \"', [3] = resultValue[index]}
       local concatted = table.concat(unconcatted);
       table.insert(resultFinal, 1, concatted);
    end
return resultFinal;
点赞
用户5697743
用户5697743

使用另一种引号来转义引号!

"'" = 表示单引号
'"' = 表示双引号
[['"']] = 双方括号内的字符串将被原封不动解析,如果有任何前导换行符,则将其丢弃
[=[I have [[ and ]] inside!]=] = 在需要多重双方括号的情况下使用 `=`, 如有必要,可添加更多 `=`

在你的情况下,

local unconcatted = {'"', resultKey[index], '": "', resultValue[index], '"'}

就可以完成任务了。如果你愿意,可以用 [[]] 替换 '

还要注意,已删除索引。当需要仅使用数组时,这是优选表示法,它与你之前的表示法效果相同,让你不必在更改时保留编号,并提高了性能。

你的 for 循环不太好。 table.maxn 在 Lua 5.1 中已被弃用,并在后续版本中已被删除。你应该使用新的长度语法:

for index = 1, #resultKey do
end

或使用 ipairs

for index,key in ipairs(resultKey) do
    local unconcatted = {'"', key, '": "', resultValue[index], '"'}
    …
end
2019-07-22 15:35:48