1
我在星號pbx中使用lua。處理json字符串時遇到以下問題。如何使用lua處理json字符串中的空值?
json將空值轉換爲lua中的函數類型。爲什麼?
如何處理這種情況?因爲我期待nil,因爲在json中沒有值意味着null,而零在lua中意味着什麼。
local json = require("json")
local inspect = require("inspect")
local myjson_str='{"Sms":{"key":"xxxxxxxxxxxxxxxxxxxxx","to":"{caller}","senderid":null,"type":"Simple","content":"Your request has been accepted in Previous Miss call. We get back to you very soon."}}'
local myjson_table = json.decode(myjson_str)
print(type(myjson_table["Sms"]["senderid"]))
print(myjson_table)
print(inspect(myjson_table))
print(json.encode(myjson_table))
出把上面是
function
table: 0xf5e770
{
Sms = {
content = "Your request has been accepted in Previous Miss call. We get back to you very soon.",
key = "xxxxxxxxxxxxxxxxxxxxx",
senderid = <function 1>,
to = "{caller}",
type = "Simple"
}
}
{"Sms":{"type":"Simple","key":"xxxxxxxxxxxxxxxxxxxxx","senderid":null,"content":"Your request has been accepted in Previous Miss call. We get back to you very soon.","to":"{caller}"}}
json.util.null是我最終發現的函數。我設法處理它。你的觀點是正確的,它取決於你使用的庫/語言。 – rajesh6115